Sunday, January 23, 2022

What is functions.php file in wordpress? 3 scenario where we can use it.

In this article we will see the functions.php file, how it works and how you can use it.

WordPress is known for being very customizable and flexible using the plugins system. But many a times we want to add the functionality to the theme or we want to add few small changes for which we do not want to add a plugin.
In that case WordPress' function.php is used. In latest premium WordPress themes the functions.php works as the main engine driving features and flexibility of the theme without requiring any external plugins, beside it is popularly used for injecting small codes like google analytics, fonts API, custom scripts, site wide hooks and filters.

Functions.php resides in the root directory of the WordPress theme. you can use all the functions of WordPress inside it just like the plugins. But as its tied to the WordPress theme, If you change the theme your changes will be lost. WordPress development guide does not recommend to directly change the core/original files of the theme which may lead to dependency hell in case of update is required, to avoid such scenario we can use child themes.
Child themes act as the modifier theme of the original theme, here we can define any change or overload the default functionalities and theme settings without changing the original theme. 

Here are five such cases where it can be used.

1)Adding Google analytics to WordPress blog

There are many ways to add google analytics to the WordPress blog, but most of the times it is unnecessory to use a plugin for that, we can use following code in functions.php file to add google analytics to the site:

<code>

<?php 
  add_action('wp_head', 'theme_prefix_add_googleanalytics');
  function wpb_add_googleanalytics() { 
   ?>
  // Insert your Google Analytics Tracking ID 
<?php } ?>

</code>

2) Displaying Post length in words

<code>
<?php 
function theme_prefix_count_post_words(){
  $content = get_post_field( 'post_content', $post->ID ); 
  $word_count = str_word_count( strip_tags( $content ) ); 
  return $word_count;
} 
</code>
This function needs to be called, we can create either a filter or modify the template file, 
for doing so, just find the files single.php(or which ever applicable), copy it and place it in the same relative position in child theme. once doe just add the following code to print the word count at desired location.
<code>
echo theme_prefix_count_post_words();
</code> 

3) Injecting the stylesheet inside header.

Suppose you want to add the bootstrap css files inside your wordpress theme, to do so using the functions.php file, Just add the following code inside the functions.php file.
<code>
function theme_prefix_enqueue_scripts() {
    // Add boot strap style css files
    wp_enqueue_style( 'bootstrap', get_stylesheet_directory_uri() . '/css/bootstrap.css', array());
    wp_enqueue_style( 'theme-style', get_stylesheet_directory_uri() . '/css/style.css', array());
    // all scripts
    wp_enqueue_script( 'bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array('jquery'), '20120206', true );
    wp_enqueue_script( 'theme-script', get_template_directory_uri() . '/js/scripts.js', array('jquery'), '20120206', true );
}
add_action( 'wp_enqueue_scripts', 'your_theme_enqueue_scripts' );
 
</code>