Last Updated on 31st May 2020 by Rob Carter
What is the functions.php file?
The functions.php file is a special file that enables you to add features and functionality to a WordPress theme. By editing the file you can enable and disable features, change how WordPress works and define your own custom functions.
Basically, it lets you add custom code to your site. It can be used to hook into the core functions of WordPress to make your theme more flexible and powerful, or to perform actions like creating custom menus and sidebars, disabling WordPress’ built-in image compression and adding Google Analytics tracking code.
The WordPress Codex describes it like this:
“The functions file behaves like a WordPress Plugin, adding features and functionality to a WordPress site through PHP code. You can use it to call native PHP functions, WordPress functions, or to define your own functions.”
Although the functions.php file behaves like a plugin in that it adds features and functionality to a website, in many ways it’s very different.
The key difference is that the functions file is tied to a specific theme. If you were to change themes, the custom code you added to the functions file would no longer be executed. For that reason, if you’re creating new features that should be available no matter what the website looks like, it would be a good idea to put them in a plugin instead.
There are also a few other differences between the functions.php file and a WordPress plugin:
The functions.php file:
- Is stored in each theme’s subdirectory, usually “/wp-content/themes/[your-theme]/”;
- Executes only when the theme is active;
- Applies only to that theme (if the theme is changed, the custom code can no longer be used);
- Can contain numerous blocks of code for many different purposes; and
- Requires no unique header text.
A WordPress plugin:
- Is stored in “/wp-content/plugins/”, usually within a subdirectory;
- Executes only when activated via the plugins panel;
- Applies to all themes;
- Should have a single, specific purpose, such as adding search engine optimisation or security features; and
- Requires unique header text.
The functions.php file is loaded after all plugin files have been loaded.
The functions file and child themes
As the functions.php file is is tied to its theme, any changes you make to it are likely to be overwritten if you update the theme.
For this reason, it’s not a good idea to edit a parent theme’s functions file (or any file). Instead, you should create a child theme (if you don’t already have one), add a functions.php file to that child theme and put your custom code in there. That way, you won’t lose your changes when you update the parent theme.
Although the child theme’s functions.php is loaded just before the parent theme’s functions.php, it doesn’t automatically override it. This means that if your parent and child themes share the same function, the one in the parent theme will override the one in the child theme.
By default, the child theme’s functions file adds to, rather than replaces, the parent theme’s functions file. However, with a bit of work you can change the order in which functions are fired – and prevent functions from being fired altogether – by overriding the parent theme’s functions.
How to edit the functions file
Editing your functions file is as easy as using a standard text editor, like TextEdit or Notepad. But before you edit anything, it’s important that you create a backup of your site and save a copy of the original, unedited functions.php file. This will enable you to restore your site if something goes wrong or you make a mistake during the editing process.
Using an FTP client and text editor
The recommended way to make changes to the functions file is to use an FTP client and a text editor.
1. Connect to your website using an FTP client like FileZilla.
2. Next, navigate to “/wp-content/themes/[your-theme]/” where your theme’s files are located.
3. Locate your theme’s functions.php file and download it to your computer.
4. Open the file in a text editor such as Sublime and make the necessary changes.
5. Finally, save the file and use your FTP client to upload it to your website, overwriting the original functions.php file.
Using the WordPress theme editor
The quickest and easiest way to make changes to the functions file is via the WordPress theme editor.
1. To access the editor, log into your WordPress admin dashboard and go to Appearance > Theme Editor.
2. On the right-hand side of the screen you’ll see a list of all the files contained in your theme (the active theme should be selected by default). These differ depending on which theme you use, but all free and premium themes will include a functions.php file. Simply click on the functions file to open it in the editor.
3. Make the necessary changes to the functions file.
4. Once you’ve finished, click on the “Update File” button to save your changes.