Posted on Leave a comment

How to Add Custom PHP Code in WordPress

WordPress is an amazing content management system and works seamlessly out of the box. It has thousands of themes and plugins to help you build any kind of website. However, often you will need to tweak the theme or plugin to suit your needs. For example, when you want to add some additional functionality to your website or tweak the theme that you are using without changing the entire theme.

WordPress themes and plugins can easily be tweaked using PHP code. You can also add new functionality to WordPress using PHP and avoid the use of plugins. Often when tweaking the code users tend to do it in the parent theme’s files. Often code is placed in the parent theme’s functions.php file.

This is not the best solution and while it may work, when you update your theme or plugin, your changes will be lost. Because of the lost changes users then tend not to update themes or plugins. This can have undesirable effects on your website. You may miss out on critical security or bug fixes that are issued usually through updates to the theme or plugin. You may also lose the changes when you upgrade your WordPress version.

It is therefore recommended to use a WordPress child theme. You can easily add code to the child theme’s functions.php file and this will no be vulnerable to updates.

Here are 3 methods of adding PHP code to your WordPress website:

  • Method 1: Adding PHP code with the built-in “Theme Editor”
  • Method 2: Adding PHP code through FTP
  • Method 3: Use a plugin

Method 1: Adding PHP code with the built-in “Theme Editor”

This method is not recommended as it can easily break your website. Often when adding code directly with the “Theme Editor”, if the code is not correct or conflicts with other resources on your website, you will end up crashing your website. If your website stops working, because you added the code with errors, you will not be able to roll back any changes. The only way to fix this will then be through FTP.

We could simply skip this method, but we decided to include it for informational purposes.

In this method we will add code to the theme’s functions.php file.

Step 1: Log in to your WordPress dashboard

Log in to your WordPress dashboard by visiting your admin URL. This will usually be your domain name followed by /wp-admin for example www.example.com/wp-admin

How to Add Custom PHP Code in WordPress - Log in to your dashboard

Step 2: Open the theme editor and the functions.php file

In your WordPress dashboard navigate to the Appearance menu and click on the Theme Editor as shown below:

How to Add Custom PHP Code in WordPress - Open theme editor

In the theme editor, search for the functions.php file on the right-hand side and click on it to open it.

How to Add Custom PHP Code in WordPress - Theme editor

Step 3: Add PHP code to the functions.php file

You can easily add code to the functions.php file. Remember to take a backup of the file before you edit it. This can be done by simply selecting all the code in the file and copying it to a text editor on your computer, for example, Notepad.

It is recommended to add code to the end of the file. Once you are done adding code to the file click on Update File. WordPress will usually check for any obvious format errors to your code before saving changes. If there are no errors the file will be updated.

How to Add Custom PHP Code in WordPress - Theme editor - Update File

This is an easy method for adding PHP code to your WordPress website. Most of the time you will need to add code to the functions.php file. However, sometimes you may need to add code to other files as well, for example, the theme’s header, in this case, you should select the header.php file instead of the functions.php file.

Adding code to a child theme’s function.php file

As mentioned, adding code to the parent theme’s files is not recommended as it is vulnerable to updates. Most beginners and new website owners tend to use this method as they are usually not aware of the effect of updating themes. When you update your theme, the code that you added will be lost.

By default, the theme editor will open the functions.php file of the active theme. If you have a child-theme, make sure it is activated. In your WordPress dashboard, navigate to Appearance > Themes and activate the child theme. An example is shown below.

How to Add Custom PHP Code in WordPress - Activating the child theme

After activating the child theme, if you navigate to Appearance > Theme Editor and select the functions.php file you will be able to modify the functions.php file of the child theme. Any theme updates will not affect the changes you make here.

Method 2: Adding PHP code through FTP

In this method, you will need to have an FTP client installed. For purposes of this tutorial, we are using the FileZilla FTP client. You can use any other FTP client that you wish. The process of downloading and uploading files through any other FTP clients will generally be similar.

Step 1: Open your FTP client and log in to your server

Before accessing your website’s files through FTP, you will need your FTP details. This will usually be the hostname, username, password, and relevant port. Enter these details in the relevant fields on FileZilla as shown. Click on Quickconnect once done.

How to Add Custom PHP Code in WordPress - Open FTP client

Step 2: Navigate to the functions.php file

The functions.php file will be located in the theme’s directory. This will usually be the public_html/wp-content/themes/relevant-theme folder. For this example, we are using the default 2020 WordPress theme.

Navigate to the theme’s folder and search for the functions.php file. Drag the file to a folder on your computer to download it.

How to Add Custom PHP Code in WordPress - Navigate to file

Step 3: Use a text editor to edit the file

Once you have downloaded the file to your computer you can then use a text editor such as Notepad or Notepad++ to edit the file.

If you are using Notepad++ you may not need to use FileZilla. As Notepad++ has an FTP plugin.

How to Add Custom PHP Code in WordPress - Notepad Plus Plus

You can easily add PHP code to the file and save it.

After making changes to the file, you will need to go back to FileZilla and re-upload the file. To re-upload the file you will simply need to drag the file from your computer to your remote directory.

How to Add Custom PHP Code in WordPress - Re-upload file

If you are uploading the file with the same file name you may be prompted if you want to replace the existing file. It is recommended to keep a copy of the original file as a backup in case something goes wrong.

Once you are done uploading the file you will have successfully added the PHP code to your WordPress website.

In the above method we have made changes to the parent theme’s functions.php file. Again, this is not recommended for reasons already mentioned.

Adding code to a child theme’s functions.php file

To access your child theme’s functions.php file, you will need to navigate to the child theme’s folder and select the functions.php file of the child theme. Usually the child theme’s folder will also be in the wp-content folder. An example is shown below.

How to Add Custom PHP Code in WordPress - Child theme in FTP

This is the recommended method of adding PHP code to your WordPress website.

Method 3: Use a plugin

There are several plugins available to help you add PHP code to your website. This is the most recommended method of adding code as the code you add by using a plugin will not be affected by theme or plugin updates.

We recommend using the Custom CSS, JS & PHP plugin. It is an excellent solution for adding not just PHP code but custom CSS and JavaScript code as well to your WordPress website. The plugin is lightweight and does not slow down your website.

Custom CSS, JS & PHP

Step 1: Installing the plugin

There are several methods to install the plugin. You can either install it from your WordPress dashboard through the WordPress repository, you can upload the plugin files through your WordPress dashboard, and you can also use FTP to upload the plugin.

The easiest method is to install the plugin through your WordPress dashboard.

In your WordPress dashboard, under the Plugins menu click on Add New.

How to Add Custom PHP Code in WordPress - Installing the plugin

Step 2: Search for the Custom CSS, JS & PHP plugin

Using the search bar search for the Custom CSS, JS & PHP plugin.

How to Add Custom PHP Code in WordPress - Search for the Custom PHP plugin

Step 3: Install and activate the plugin

Once you find the plugin click on Install Now.

How to Add Custom PHP Code in WordPress - Install Custom PHP plugin

After the installation is done, click on Activate to start using the plugin.

How to Add Custom PHP Code in WordPress - Activate Custom PHP plugin

Step 4: Adding code using the plugin

Once the plugin has been activated, you will find 3 new sub-menus under the tools menu in your WordPress dashboard: Custom CSS, Custom JS, and Custom PHP.

Depending on the code that you want to add click on the relevant link. For this example, we will add PHP code. Click on the Custom PHP link.

How to Add Custom PHP Code in WordPress - Custom PHP link

You can now add custom PHP code to your website. Once you are done adding the code click on Save changes.

How to Add Custom PHP Code in WordPress - Custom PHP code

Conclusion

We have gone through 3 methods of adding PHP code to your WordPress website. Editing parent theme’s functions.php file in the first 2 methods is vulnerable to theme updates and you will lose your changes once you update the theme. You can also use a child theme to add PHP code to your website, however, that is slightly more complex and for advanced users. The 3rd method of using a plugin to add PHP code is highly recommended.

There are several helpful PHP code snippets available on our blog. Here are a few notable ones:

Leave a Reply

Your email address will not be published.