When you want to change the way your WordPress theme works or troubleshoot problems with an existing theme, you’ll need to access and edit the theme’s ‘functions.php’ file.
The ‘functions.php’ file is perhaps the most complex and important part of any WordPress theme since it allows you or the original developer of that template to define all of the custom functions it contains.
In this article, I’ll explain everything you need to know about the WordPress ‘functions php’ file:
- What does ‘functions.php’ do?
- Where is ‘functions.php’?
- How can you use ‘functions.php’ to create functions?
- How does ‘functions.php’ differ from using plugins?
1. What does ‘functions.php’ Do?
The WordPress ‘functions php’ file is designed to add defined snippets of php code to your theme, which in turn add functions to, or change the appearance of, your website.
This file does not add any data or content to your page, but rather just affects the way your site presents itself.
Thus, if you were to change your theme and the code contained in the ‘functions.php’ files with it, you would change the way your site looks and how users interact with it but not the content of the site itself.
2. Where is ‘functions.php’?
Before you can even begin to think about using the ‘functions.php’ file to change the look and feel of your WordPress site, you definitely need to know where it is.
Almost every theme in WordPress has its own ‘functions.php’ file, and child themes may have additional php code files of their own.
WordPress ‘functions php’ file is typically stored in the theme’s file directory at ‘wp-content/themes/your_theme’, where ‘your_theme’ is the theme for which you want to access the file. This can be accessed from your hosting provider's file manager.
You can also access ‘functions.php’ from within WordPress by navigating to ‘Appearance > Editor’. From here, you can edit the code for any theme you have installed, whether active or inactive.
There should only be one ‘functions.php’ file per theme, although some WordPress themes do not have a code file at all.
If there's no file, you can create one by adding a new plain-text document to ‘wp-content/themes/your_theme’ and saving it with the name ‘functions.php’.
3. How Can You Use ‘functions.php’ to Create Functions?
The number and types of changes you can make to your site with a WordPress functions php file are virtually unlimited. Some examples of how ‘functions.php’ creates functions and makes changes to your theme within are:
- Defines the sizes, in pixels, of ‘small’, ‘medium’, and ‘large’ images on every page
- Tells WordPress that your site has a main navigation bar and any additional secondary navigation panels
- Changes the fonts used throughout your site
- Creates widget areas across different portions of your site
It’s easy to start editing your ‘functions.php’ to create functions above and beyond what is available in your current theme.
However, since it's the code controlling how your website looks and feels, errors in your code as small as a missing comma can bring down parts or even all of your site.
It is important to edit the ‘functions.php’ file with care and to backup your WordPress site.
Another good practice is to save a copy of the ‘functions.php’ file, from which you can restore the original file without re-installing the theme if anything goes wrong.
Depending on your theme, the ‘functions.php’ file will have text notes throughout that tell you what each line of code does. If you simply want to make changes to existing features, such as image sizes or fonts, you can likely edit existing lines of php code rather than add new lines of code.
If you want to add entirely new functions, you’ll need to delve into the world of coding in php.
While you may be able to find examples of how to add specific functions online, beware that the code for every theme is different and any functions can interact or interfere with the functions that are already used.
Another important consideration when adding code is to keep that added functionality organized and to make notes about what it is intended to accomplish. Otherwise, the file can quickly become chaotic and it can be extremely difficult to identify any errors that appear later.
4. How does ‘functions.php’ Differ from Using Plugins?
At this point, you may be thinking that using php to create functions is a lot like using plugins to change the look and feel of your WordPress site.
In many cases, if you can find the appropriate plugin, you can achieve the same end goal. However, there is an important difference between the two methods: the ‘functions.php’ file follows a specific theme, whereas plugins follow your entire site.
This means that when you change themes, the ‘functions.php’ file from your old theme will be deactivated and from the new theme will be activated.
This is because the ‘functions.php’ file holds a special place in WordPress. The software knows to automatically look for it and run the code in the active theme, but WP will not run the code of ‘functions.php’ files in any deactivated themes.
Thus, when changing themes, you will need to transfer any custom php code you added to the old ‘functions.php’ file to the new one.
You will also need to adjust that code to the existing php in the new ‘functions.php’ file, which can be quite a difficult task if the two themes are very different or if you added a lot of code to the original file.
On the other hand, plugins work across themes without you needing to make any changes to the back-end code.
While this may be easier in many cases, you may not like the way that a plugin works on the new theme or you may not be able to find the right plugin for the changes you want to make.
A middle-ground alternative is to use a plugin that automatically adds php code you create to the ‘functions.php’ file of your active theme and works even when you switch themes.
Managing WordPress 'functions php' File - Conclusion
The ‘functions php’ file is an extremely powerful WordPress tool contained within every WordPress theme.
Similar to plugins, it can be used to change the look and feel of your website, but unlike plugins, it can be modified with custom code and follows a theme rather than your site as a whole.
Importantly, remember to save a clean copy of your ‘functions.php’ file and to back up your WordPress site prior to making any changes.
Did this guide help you better understand the WordPress ‘functions php’ file and to start making changes to your site? I sure hope so - and I'd love to hear your thoughts on that.