1. Web Design
  2. WordPress
  3. WordPress Themes

5 Uses of the functions.php File in WordPress

Scroll to top
Read Time: 5 min

There are only two files that you need to use when creating a WordPress theme: the index.php file, which acts as the main template file for your website, and the style.css file, which is the main styling file for your website. There is also a third file called functions.php that is not actually required but plays an important role in a theme nonetheless.

In this tutorial, our focus will be on learning what the functions.php file is, some of its common uses, and when you should be using it instead of creating plugins. Let's get started.

What Is the functions.php File?

The functions.php file in WordPress is used to add new functionality or features to your WordPress website. You can write PHP code in this file. This code can either define your own custom functions or make calls to existing WordPress functions. We will learn how to add new features to your WordPress website through the functions.php file in the next section.

It is entirely possible for a WordPress installation to have multiple themes. Each of these themes will have its own functions.php file. However, only the code inside the functions.php file of your active theme actually runs when someone loads your website.

You will need to find your functions.php file before you can edit it. You can find it in the /wp-content/themes/theme-name/ directory. Here, theme-name is the name of any theme that you have installed and activated.

Any child themes that you have installed on your website can also have their own functions.php files. Unlike other themes, the functions.php file of the child theme won't override the functions.php file of the parent theme. It will actually add to the functionality that the parent theme provides.

Uses of the functions.php File

There are a lot of things that you can do with the functions.php file. We will learn about some of those things here.

Enqueuing Scripts and Styles

You might want to include additional scripts and styles with your website to load on your front end. The best way to achieve this is with the help of the wp_enqueue_scripts hook. Contrary to its name, this hook is useful for enqueuing both scripts and styles. Here is an example of using it in your functions.php file.

function monty_scripts_styles() {
    wp_enqueue_style( 'my-style', get_template_directory_uri() . '/css/custom-style.css', array(), '1.2.0', 'all' );
    wp_enqueue_script( 'my-script', get_template_directory_uri() . '/js/custom-script.js', array( 'jquery' ), '1.5.6', true );
add_action( 'wp_enqueue_scripts', 'monty_scripts_styles' );

Using the wp_enqueue_scripts hook to add your styles and scripts improves performance by making sure that files aren't loaded multiple times, among other things. You can read this tutorial about loading your CSS into WordPress the right way to learn more.

The wp_enqueue_style() and wp_enqueue_script() functions accept multiple parameters to specify things like the source, version, dependencies, etc.

Creating Custom Shortcodes

WordPress shortcodes help people add dynamic or static content to their website without needing to write complex code. Think of things like a list of latest posts, some images from the gallery, the current date, or a simple greeting.

You can write a bit of code in the functions.php file to add a custom shortcode.

function monty_greeting_shortcode() {
    $current_user = wp_get_current_user();
    if($current_user) {
        $name = $current_user->display_name;
    } else {
        $name = 'Guest';
    $greeting = 'Hello, '.$name.'!';
    return $greeting;
add_shortcode( 'greet_readers', 'monty_greeting_shortcode' );

This is a very simple example where you can use the greet_readers shortcode anywhere on the front end to add the greeting Hello, Display Name! for logged-in users and Hello, Guest! for everyone else.

Now try writing your own shortcode that returns the content you want to display.

Remove the WordPress Version Number

You might be interested in checking the WordPress version of your website for a variety of reasons.

However, this information shouldn't be visible publicly. WordPress adds a generator meta tag that publicly displays the currently installed WordPress version on your website in the HTML source code. You can remove the generator tag by simply adding the following line to your functions.php file.

add_action( 'wp_head', 'wp_generator');

You can consider using the following line if you want to remove the information from other places like RSS feeds as well.

add_filter('the_generator', '__return_empty_string');

The built-in __return_empty_string() function will return an empty string and prevent your version information from being displayed on the front end.

Disable the WordPress Admin Toolbar

WordPress adds an admin toolbar at the top on the front end for all logged-in users by default. You can add the following line in your functions.php file to disable it for everyone at once.

add_filter( 'show_admin_bar', '__return_false' );

Please keep in mind that the WordPress admin toolbar cannot be disabled on the back end.

Disable Automatic WordPress Updates

While it isn't usually recommended for you to disable automatic WordPress updates on your website, there are multiple reasons why you might want to do so, such as preventing unexpected breakdowns. You should read this tutorial to learn more about WordPress automatic updates.

Add the following lines to your functions.php file if you are sure that you want to disable automatic WordPress updates.

add_filter('auto_update_core', '__return_false');
add_filter('auto_update_theme', '__return_false');
add_filter('auto_update_plugin', '__return_false');

These three lines will disable core updates, theme updates, and plugin updates respectively.

Final Thoughts

In this tutorial, we learned a lot of things about the functions.php file. We now know what this file is, where it is located, and how we can use it to add new functionality to our website. It is also possible to add functionality to your website with plugins. So what is the best way to do this?

You should consider using the functions.php file if the functionality you are adding is theme-specific or if you just want to add a tiny bit of new functionality. Creating and installing new plugins for every little thing will be detrimental to your website's performance.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.