SHARE

What is function file

Function file is a theme file in wordpress theme. Each wordpress theme has a unique function.php file. All of theme functions are added in this file . These functions are only executed when specific theme is installed.

User can add many functions in  function.php file. Function file is location in wp-content/themes/theme-name/function.php . 

function.php file in wordpress
function.php file in wordpress

Functions which are inserted in function.php only executed when theme is activated.

For example : theme directory has four themes with theme name twenty thirteen theme, twenty fourteen theme, twenty twelve theme and goodies theme. Each theme has own (function.php) function file.

When “twenty thirteen” theme is activated , then function.php file located under wp-content/themes/twenty-thirteen/function.php is executed .All the function under this file are run.

Feature of function file (function.php)

  • function.php has no requires unique Header text.
  • function.php is stored in theme’s sub directory.
  • It executes only when the specific theme is activated. For example : theme directory has four themes twenty thirteen theme, twenty fourteen theme, twenty twelve theme
  • It Applies only to that theme. If the Theme is changed, the functionality is lost this theme.

Structure of function.php

function.php structure is quite simple. It starts with <?php  and ends with ?> all of functions are inserted between these two codes. Example of function file.

<?php
// add social media
function social_media() {
if (is_single()) { global $post; echo '<div class="social-post"> <div class="counter-twitter"><a data-related="DIY_WP_Blog" href="http://twitter.com/share" class="twitter-share-button" data-text="' . get_the_title($post->ID) . ' —" data-url="' . get_permalink($post->ID) . '" data-count="vertical">Tweet</a></div>' . "\n";
?> <div class="counter-fb-like"> <div id="fb-root"></div><fb:like layout="box_count" href="<?php the_permalink(); ?>" send="false" width="50" show_faces="false"></fb:like> </div> <div class="counter-google-one"><g:plusone size="tall" href="<?php the_permalink(); ?>"></g:plusone></div>
</div> <?php }
}
?>

In this function file (function.php )example, a simple function is created to tweet from webpage. Create a simple function.  Function code is

// add social media
function social_media() {
if (is_single()) { global $post; echo '</pre>
<div class="social-post">
<div class="counter-twitter"><a class="twitter-share-button" href="http://twitter.com/share" data-related="DIY_WP_Blog" data-text="' . get_the_title($post-&gt;ID) . ' —" data-url="' . get_permalink($post-&gt;ID) . '" data-count="vertical">Tweet</a></div>
' . "\n"; ?&gt;
<div class="counter-fb-like">
<div id="fb-root"></div>
&nbsp;
</div>
&nbsp;
<div class="counter-google-one"></div>
</div>
&lt;?php } }

and simply copy and paste this in just between

 <?php     <-- paste function here-->  ?>

User can add multiple functions in function file (function.php) . It makes the theme versatile and dynamic with simple functions.