It is common to find in WPHelp many tricks to add functions to the functions.php file, we have even talked about whether it is better to use this file or a utility plugin, which we also learned to do, but what is the functions.php file and how to use it?
Let’s look at some basic concepts and tips on how to use it, shall we?
What is the functions.php file?
The most basic thing is that it is a PHP file, that is, a text file full of characters and spaces that the PHP engine will run to do things on your website. These characters are called functions and are enclosed between the tags. The thing is simple: what is inside those tags is executed and what is outside is not.
It is the natural, and better structured, replacement of the old my-hacks.php file, which disappeared in WordPress 2.8, where extra features were included in the past, but with the aggravating factor that the file was crushed with every WordPress update.
Where is the functions.php file?
All current WordPress themes have a functions.php file in the main folder, and if you don’t have it you should have it. Sometimes it contains many functions and sometimes only a few. You access it through Appearance > Theme Editor in the WordPress Dashboard
What does the functions.php file do?
The functions.php file contains PHP codes called functions, a standard WordPress feature that allows a theme to “connect” to internal WordPress functionality (register sidebars, add thumbnail support, etc.) or to the active theme itself, such as specialized short codes (shortcodes).
How does WordPress communicate with the functions.php file?
WordPress, by default, expects to find a functions.php file in the active theme folder, it’s okay if it doesn’t find it, but WordPress checks to see if it exists and interacts with it, executing the functions it finds.
In this way, WordPress “knows” that the functions in your theme’s functions.php file must be executed and will use the PHP engine installed on the server and WordPress’ own standard functions to interpret and execute them. It sounds complicated but it’s simple, based on the functions themselves and other pieces of code called “hooks”.
Is a functions.php file necessary?
It is not essential but it is highly recommended. You can include in it functions that are necessary for the theme to work properly, and since it is the first thing WordPress checks, they will be loaded immediately, before the plugin codes, because in the end, the first thing that must be shown is the design is the theme.
Tips for using the functions.php file
Although it is interesting to have a functions.php file in the active theme, there are some tips to keep in mind:
- Don’t go crazy adding functions to the functions.php file. This file is great for including functions related to design, with visual elements, for the rest of things is better a utility plugin. There are a lot of people who use the functions.php file to put in it all the cool functions you find in WPHelp or any other website, and no, it is not a drawer, every function you include must have a meaning and purpose, and if you don’t use it delete it.
- Always add the new code at the end, to avoid breaking the theme’s own functions.
- Add before the code a line commenting what it does (Ex.: // This code changes the size of the excerpts). It will help you remember what each code does in case you have to modify or remove it one day.
- The functions.php file of a child theme does not overlap with the parent theme, unlike the other files you create in the child theme. It will be executed before the parent theme, which will be a problem if there are the same or incompatible functions, which will result in ugly PHP errors of duplicate functions or something much worse, so check what you put in the functions.php file of the child theme and the parent theme.
- Make sure it doesn’t grow too much, even if you need a lot of functions. If you see that your functions.php file starts to border on the 200 lines, seriously consider creating dependent files from which your functions.php file extracts the rest of the code you want to execute, you can easily do this by using PHP commands such as
require(), which will include or make the necessary connection to execute everything without overloading your functions.php file.
As you will see, the functions.php file is very useful, and used with moderation it will expand and improve the functionality of your web, so learn how to use it, treat it with care and add useful functions.
Read this post in Spanish: Qué es y cómo se usa el fichero functions.php