In many posts we publish here, and in many others you can find on the internet, you will see that the file “functions.php” is often mentioned, also with some instructions on how to insert code into it.
functions.php is a very important file that is in every theme folder, although it is actually not always there, and it’s the place where you add all the extra features you want to give to your website to make it more customized, unique and to your liking. In fact, it behaves like a plugin.
- Change the email for new users.
- Hide menus on the WordPress dashboard.
- Change the behavior of the website when you log out.
- Hide the “Add to cart” button on certain pages.
- Improve the performance of WordPress.
There are already plugins in the WordPress repository for all of these examples. You might be wondering why you would want to write what you need in this file when there is already a plugin? There are two main reasons for this:
- The examples above are quite simple, but experienced users and developers can use functions.php for more complicated things for which there is no plugin. For example, to remove the spaces in the usernames that WPForms creates during user registration. Let’s see if you can find a plugin for that!
- Usually, what you write in functions.php does not affect your site’s performance as much as a plugin, as you are more likely to find a plugin that does a lot more than what you are looking for. This means that you would be installing a heavier plugin than necessary.
functions.php vs plugin
Theoretically, with some programming knowledge, you can create a plugin that has the same content as the functions.php file. However, there are some technical and semantic differences.
- is created for a single purpose.
- is located in the path
- runs every time a page on your website is accessed as long as the plugin is active.
The file functions.php…
- can contain several code blocks for different purposes.
- is located in the path
/wp-content/themes/theme, where “theme” is the name of the theme you are currently using.
- runs every time a page is accessed on your website, and only if the theme it belongs to is active.
We personally like to use both methods: If what we want to add is related to the theme, we put it in functions.php. If not, we put it in a plugin in case we want to change the theme in the future, so we don’t lose the custom code.
How to edit the functions.php file
First create a child theme
It is not necessary, but recommended, that the functions.php file you are editing is located in the folder of your child theme. What is a child theme? It is a theme that inherits the functionality of the parent theme. Why does it exist? Basically to be able to change the files of the theme without overwriting the originals. The big advantage is that the changes you make to it, including changes to functions.php, are preserved when you update the parent theme.
This is what a child theme looks like in a file explorer:
In it you will find the following:
The child theme contains the style.css file to indicate where the parent theme is located and the functions.php file, which we will edit.
The process of creating a child theme is usually the same for all themes, but sometimes there are small differences, so we recommend contacting the author of your theme to show you how to do it.
And then use a file editor
Or an FTP client like FileZilla. Regardless of whether your hosting provider offers a file editor or you use an FTP client, you need to go to the path /wp-content/themes/child-theme. In the screenshots above, for example, you can see that our child theme is “blocksy-child”.
Open the file with a text editor, copy the desired code and paste it at the end of the file. For example, this disables the WordPress admin toolbar.
// Remove top bar add_filter( 'show_admin_bar', '__return_false' );