How to add Custom PHP code

How to Add Custom PHP Code to WordPress Sites Manually.

Adding functions, hooks, or any other custom PHP code to the WordPress site is easy. WordPress has a default editor to modify code of your themes and plugins. And you can also add PHP code with a plugin. Let’s start with the manual system.

Login to your WordPress site, go to Appearance > Theme Editor. Initially, it warns you because you are making direct edit to your theme file. Click on I Understand and you will be taken to the theme editor.

Add Custom PHP Code to WordPress Sites manually

Select the function.php file on your left and place your PHP code at the bottom. Finally, click on “Update File” finish up adding code to the function.php file. This way you can edit other theme files too.

Note:  WordPress does not recommend editing files on the default editor this way. You may run into errors and end up breaking your site. And every time you update your theme, any modification you made to these files will be gone. You have to add code again after every update.

With a “Code snippets” plugin

Another plugin that can help you add PHP in WordPress is the free Code Snippets plugin.

With this plugin, you can easily add general code snippets to your site by going to Code Snippets → Add New. When you add your PHP code snippet, you can choose between running it everywhere or only on the frontend or backend of your site:

Adding PHP with the free Code Snippets plugin

In addition to adding PHP code to WordPress, the plugin can also help you add CSS, HTML, or JavaScript.

With a “My Custom Functions” Plugin

You can add functions, hooks, or other custom PHP code to WordPress sites with a plugin too. And that is the safest way. Because, in this case, you will add PHP code without editing files. And you no need to do it again and again after every update. We will do it with My Custom Functions WordPress plugin.

Step 1: Install and Activate My Custom Functions Plugin

Add Custom PHP Code to WordPress Sites

When you are logged in, Plugins > Add New on your Dashboard. Search My Custom Functions in the search and click Install and Activate subsequently.

Step 2: Add Custom PHP Code to Your WordPress Site

Once you have installed and activated My Custom Functions plugin. You need to go to Settings > PHP Inserter on your Dashboard.

Add Custom PHP Code to WordPress Sites with a plugin

Place your code under “Functions (PHP code)” and turn on the toggle on your left. Click on Save Changes to make your code work.

Note: Do not wrap your custom PHP code in HTML tags, such as <?php…?>. If your code has no error it should work flawlessly. And in case of any website crash follow the author’s instructions.

Warning: Even missing a single semicolon at the end of your code can break your site. Please be careful while writing code. It is best to try your code on a WordPress site installed on your local computer and make sure it gives you the desired result. Then add your code to the live site. Again please be careful.

Leave A Comment?