When you are working with WordPress, sooner or later you will probably get in contact with the functions.php file. For example in a WordPress tutorial you are asked to add a code snippet to the functions.php of your Theme. When you are a WordPress beginner often times questions arise around this file. What is it, where do I find it and how do I use that file? With the help of this Quick Tip you will have answers to these questions 🙂
What is the functions.php in WordPress
The functions.php file is a file in your WordPress theme. In general this file should already be in your theme but this is not a must (we show you how to create that file later). As the name suggests the task of the functions.php is to provide theme functions and features.
You can think of the functions.php as some sort of WordPress Plugin to enhance or modify the default functionality of WordPress. Consequently the use cases on what to do with the functions.php are as endless as the shear amount of WordPress Plugins. For Example you can use the functions.php to edit the length of an excerpt, create new post types, use shortcodes, add widgets and many many more things.
Here is an example to edit the content width of your Theme with the help of the functions.php
<? /** * Limit the content width to 800px */ if ( ! isset( $content_width ) ) $content_width = 800;
However unlike regular Plugins the functions.php gets automatically activated with the Theme that is currently running. This means you do not have to activate it in the WordPress Dashboard. Furthermore WordPress Plugins are usually only serving one single purpose. While in the functions.php many blocks of code can be used for numerous different purposes.
The functions.php file is glued to one Theme. This is important to remember. Changes made in a functions.php of a certain Theme do not affect other Themes. So consequently only the code of the active Theme’s functions.php is actually running.
From the file ending (php) we see that the code in the functions.php file uses PHP as language. You do not have to be a PHP Pro to work with the functions.php but it’s definitely better to have a decent knowledge on how to write code when working with this file.
Where to find the functions.php for your WordPress Theme
You find the functions.php (if already existing) in the root directory of your Theme (e.g.
wp-content/themes/<your theme name>). A Child Theme can also contain a functions.php.
Working with the functions.php
Let’s say you have found a code snippet which modifies your WordPress Theme in a certain way. The instructions of that code snippet say that you have to add the code to your functions.php. So what do you actually have to do?
- When your Theme already has a functions.php file: Open up the existing functions.php and at the end of the file add your code snippet. This should already do the trick.
- When your Theme does not have a functions.php file yet: Go to your Theme folder and create a file called functions with php as file ending. Open up the file. Now even though the file already has the ending php, in the first line of your new file we have to declare that this file will contain a PHP code block. Write
<?phpfor this. Now you can add your code snippet.
Hint: Even though this method works, it is not best pratice to do so. It is better to either use the functions.php of a Child Theme or to create a seperate Plugin for your code snippet.
In the following we list some best practices when working with the functions.php.
- Use the functions.php of your Child Theme: There are several reasons for that. An update for your Theme also overwrites the functions.php file. This means if you have customized the functions.php of your Theme, these changes will be lost. The better option is to put modifications for your Theme into the Child Theme. The functions.php of your Child Theme does not get overwritten.
- Remember that code in the functions.php of your Theme only applies to that Theme: We have already learned that the functions.php behaves like a Plugin but it is bound to a Theme. When you want to create features that should be available no matter which Theme, it is best practice to create a plugin instead.
- Coding errors in your functions.php can break your Theme and lock you out of your site: Even a missing semicolon in your code can break your site. Consequently you should know what you are doing when modifying the functions.php.
Hint: When all of this scared you but you really want to add some extra functionality to your WordPress theme we recommend the following. Use WordPress Plugins that are especially made for this scenario. One is Code Snippets.
- Official Theme Handbook from WordPress.org about the functions.php
- Library of code snippets for the functions.php from http://wp-functions.com
We hope you could learn something new with our Quick Tip. You should now be able to properly add code snippets to your beloved Theme. We wish you a great day 🙂