Each WordPress theme comes with a function.php or the theme functions file. In this post I’ll explain where to find it, and why it’s better not to edit it directly.
Even if each theme has its own functions.php file, it’s just the active theme functions.php file that is activated when the site is loaded.
Inside functions.php this is where you can define custom code snippets (PHP) to add new features to the site or change the default ones. With functions.phpyou can do things like the following:
- insert header and footer scripts
- specify a custom message to show on the WordPress dashboard
- use WordPress hooks like
excerpt_length(to change the length of your post excerpt from the default word count)
- include features such as post thumbnails, navigation menus, and so on, through the use of
- specify features to reuse in various theme template files and many more
Once added, the custom function will immediately be applied to the site theme and you will see the changes the next time your site loads.
Where found functions.php?
The functions.php the file is located in / wp-content / themes / folder inside the WordPress installation directory, inside the folder for the currently active theme.
You can edit the file and add code snippets for any custom functions you want to add to your site. However, this is something that should generally be avoided. functions.php it is part of the theme and if you change it to a third party theme, there are several risks. For one, you risk losing file changes when you update your theme or change it. Worse still, a mistake in the code could lead to a website critical error, effectively locking you out of your site.
If you want to change the functions.php file for a third party theme, the best thing to do is create a child theme. Aside from that, simply avoid editing the file functions.php file. Learn more about child themes and override functions.php here on Envato Tuts +.
There are other safer ways to add code snippets to your site’s theme functions file. I’ll discuss one in the next section.
Recommended way to add custom snippets a functions.php
Instead of editing directly functions.phpuse a code snipper plugin like WPCode to easily add custom features to your WordPress site using code snippets, without touching the theme functions.php file.
The plugin allows you to add your own code snippets and, in addition, it provides you with a built-in library of code snippets from which you can find some of the most popular snippets to use on your site, saving you the hassle of downloading plugins separately. .
You can also search for other ready-made plugins by categories, which are listed alphabetically.
Under Snippets of code, you will find the list of code snippets already activated for your site. To add a new snippet, click Add new button.
You will be led to the Add snippets page to select one of their predefined templates or start from scratch with a blank snippet.
If you choose a new (empty) snippet, you will be directed to the page shown below. There you will provide the name of your code snippet in the title box and then copy the code snippet into the code block. You will also need to specify the code type (should be PHP)
Click the Save snippet button when finished. The code snippet will be added to your theme’s function file and applied to your site’s theme.
Each WordPress theme comes with a functions.php file that is used to add custom features and functionality to the current site theme, as well as to change default features.
In general, you should avoid editing functions.php for a third party theme. This is to avoid losing the content of the useful file (when editing or updating the theme) and instigating a critical error in your site. Instead, use a code snippet plugin like WPCode to add custom snippets.