WordPress allows you to extend its functionality by using its simple API. You can either create a custom plugin with your custom code or you can add your code to your theme’s functions.php file.
In this guide, I’ll show you how to do the latter…
Where Is The functions.php File Located?
WordPress allows you to customize all aspects of its behavior. You can do this using a plugin or using your current theme’s functions.php file. This file is just as powerful as a plugin and allows you to add whatever functionality you want to your website.
To add custom code to your website, you need to add it to your currently-active theme’s functions.php file. You can find this file in the root directory of the theme.
WARNING: You should only add code to a theme’s functions.php file if it’s a custom theme that you built yourself. Otherwise, as soon as you update the theme, all your changes to the functions.php file will be lost. This is why it’s recommended that you instead use a custom plugin to add new functionality to your WordPress website. As those changes will persist even if you change your site’s theme.
How To Add Your Custom Code To Your Theme’s functions.php File From WordPress Dashboard
There are many ways to add custom code to your theme’s functions.php file.
The easiest way to do this is through the Theme File Editor tool of your WordPress website:
This tool lets you edit all your theme files. On the top right, you can select the theme whose files you want to edit:
And the right panel allows you to select the file you want to edit.
Select the functions.php file.
Now, scroll down the file’s code and add your custom code to the bottom of the file:
Click the Update File button to save the code.
WARNING: Although this is the easiest way to edit your theme’s functions.php file, it’s not recommended. You should rather use something like FTP or SFTP or any file manager tool offered by your web host.
If you edit your functions.php file through WordPress and your code breaks something, then you won’t be able to remove your faulty code. But with FTP or some other similar method, you can remove the faulty code easily even if your website is down.
Another way is to create a child theme (see the second last section).
Better Method: Edit The functions.php File From FTP
If you edit your functions.php file from FTP and something breaks, you can easily revert the changes and go back to a working version of your website. But if you edit it from WordPress, if something breaks, you won’t be able to do anything.
First, log into your FTP server. You will find FTP login credentials in your web host’s control panel (dashboard). Most web hosting providers offer cPanel. If you have access to cPanel, then you will need to first log into that and create an FTP account.
If you are using a VPS server, like I am, you’ll need to log into SFTP via a tool like Bitvise.
I’ll show you how to do it using SFTP but the instructions for FTP are exactly the same.
Once you are logged in, navigate to where you have installed WordPress:
Now, navigate to the themes folder inside wp-content and then to the folder of the theme that you are using:
Now, right-click the functions.php file and select the Edit option.
You can now edit your file in your operating system’s default editor. When you are done making changes, just save the file and your FTP or SFTP client will upload the changes to your server.
THE BEST WAY To Edit Your functions.php File
The best way to make changes to a theme’s functionality is by creating a child theme. This of course doesn’t always make sense if you are using a custom theme. You can, most of the time, directly make changes to your custom theme. But if you are using a theme that you didn’t create, then it makes sense to create a child theme.
A child theme simply overwrites parts of its parent theme. This way, you only change the things you want to change and make sure that your changes don’t get overwritten when the parent theme is updated.
To create a child theme for the theme that you are using, use our free child theme generator.
You can easily add new functionality to your WordPress site by editing your theme’s functions.php file. In this guide, I showed you the easiest way you can do so. But remember, it’s not the best way. And something like FTP is more suitable for the job if it’s available to you.