I already talked about how to add custom CSS to your site. It’s now time to learn how to add custom PHP code as well!

In most of my posts on this website, I tell you to add code to the functions.php file, how should you do it? And what is the best way to do it?

What Is The functions.php File

The file functions.php is a file required for all WordPress themes. A theme can’t exist without it.
It has to be located in the root of your theme, so in wp-content/themes/your-theme-name/ where your-theme-name is the name of your theme. For example in the case of Storefront the file is located in wp-content/themes/storefront/.

You can find more detailed info about this file here.

Adding Custom PHP To functions.php

Now you know what the functions.php file is, but how can you edit it?
There are two ways to do it. The first one is from the Dashboard, the other one is via FTP.

To edit the file from the Dashboard, go to Appearance > Editor. You will see the editing screen of the active theme. On the right there’s the list of all the theme files, search for the one named Theme Functions (functions.php). Click on it to open it in the editor and then edit it.

Location of the functions.php file in TwentyTwelve

This screenshot shows where to find the file functions.php in the theme TwentyTwelve to add custom PHP code in it.

WARNING: Editing this file in the Dashboard is risky. If there’s any error in your code, being it a typo or anything else, your website will break upon saving the file. To fix it, if it happens, you will have to open the functions.php file via FTP and fix the broken code.

The other way to edit the file is via FTP. By using your favourite FTP program, such as FileZilla or Transmit, connect to your site and find the root of your WordPress installation. It is usually located in the folder public_html.

When you are in the root of your WordPress installation, navigate to wp-content/themes/your-theme-name/ to find the functions.php file. Right click on it, you should have an item named Edit, click on it to open the file in your code editor and make changes.

Using a Child Theme

As you might guess, editing the file functions.php directly in your theme is not a good idea. The theme author will release an update soon or later, and you will lose all your changes when updating the theme on your site.

In this case, a child theme is your best option. Read here how to create a child theme, then follow the procedure above, but you will find the file in wp-content/themes/your-child-theme-name/.

You could now say “But I’m already using a child theme and it gets updates!”. Ok, that’s fine, I have a solution for you, keep reading!

Using a Plugin

Sometimes you buy/download child themes for your theme. For example Storefront has many premium child themes.

In this cases, the option above to use a child theme is not possible because your child theme, like the parent theme, is updated by the developers. You would lose your changes when updating it.

You can use a plugin. Download and install the Theme Customizations plugin in your website. Via FTP open the file functions.php located in wp-content/plugins/theme-customisations/custom/ and add your custom PHP code there.

This plugin will never receive updates because there’s no need to update anything.

Customize Your Website

Now you know everything you need to safely customize your website. Go and do it!

Happy customizing!

1 reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.