best method to insert custom code in WordPress

Do you want to add custom PHP code to your WordPress website?

When you read articles/guides related to WordPress, oftentimes you can see they suggest you add PHP code to tweak an existing feature of WordPress or add a new feature/option.

Apart from articles, you may also need to add your own enhancement to your site directly with PHP.

There are multiple methods to add custom PHP code to WordPress,

  1. Using a 3rd party plugin to insert PHP scripts
  2. Inserting the code in the theme’s function.php file
  3. Writing a simple site-specific plugin

Which is the best method to insert PHP code in WordPress?

The best method to insert PHP code easily in WordPress is using a “site-specific” plugin.

In this article, I’ll show what is a site-specific WordPress plugin and why it is the best method to add custom PHP code to WordPress.

What is a site-specific plugin?

A site-specific WordPress plugin is just a WordPress plugin that you yourself write and activate on your website.

It contains all the custom code you want to execute on your website?

Don’t be afraid to think about writing your own WordPress plugin. It is very easy to write your WordPress plugin.

Why insert code using a site-specific WordPress plugin?

Let’s go over the advantages and disadvantages of all the methods I have mentioned above. That would explain why a site-specific WordPress plugin is the best way.

Method 1 – Using a 3rd part plugin to insert PHP code

There are multiple WordPress plugins in the WordPress repository which you can simply install on your website. 

These plugins provide an admin UI where you can paste your PHP code just like how we write posts. It is beginner-friendly.

Code snippets is one such plugin that allows us to do this. These plugins have a feature that inserts the code only when there are no syntax errors. So it protects your website from going down in case of an error with your PHP code.

Code snippets plugin in action where a custom PHP code is added using the plugin

The main drawback of this method is that it adds unnecessary overhead to your website. 

Your PHP code is also saved to the DB. This might be an advantage since your code is safe when your DB is backed up. But with the site-specific plugin approach, certain WP backup plugins offer features to backup the plugins itself.

Why add a whole 3rd party plugin to your code just to manage some small custom PHP code?

Some plugins, generate separate PHP files for every custom PHP code you paste in the backend and load them later. This is what I mean by overhead.

Though these plugins don’t decrease site performance, I think this can be entirely avoided when we can do it on our own using site-specific plugins.

It just makes things simple, full control on us, no need to pay for something, worry about plugin upgrades.

Advantages

  • Beginner-friendly
  • Code is inserted only when there are no syntax issues.
  • Manage custom PHP code just like posts

Disadvantages

  • Adds unnecessary overhead to your site.

Method 2 – Inserting the code in the theme’s function.php file

Any WordPress website needs one theme to be active all the time. All themes have a file named “function.php”.

This file can be found in your theme folder. It contains PHP code that your theme needs.

We can edit this file within our WordPress admin dashboard.

Simply go to Themes > Theme editor and select the “Theme functions/function.php” file.

You can see the entire source code of the functions.php file. In this file, you can paste your custom PHP code before ?>.

Custom code added to WordPress theme's function.php file
Custom code added to WordPress theme’s function.php file

WordPress will execute your PHP code since it is part of the theme’s PHP file. Just ensure it is the active theme.

Now, what are the disadvantages of this method?

As you can guess, we making a change directly on a theme’s source file.

When the theme gets an update the code you have pasted will be removed. Also, when you switch themes the custom code won’t be executed. You can avoid this by creating your own child theme and making changes to that instead.

Advantages

  • Easy to add custom code quickly.

Disadvantages

  • Custom code will be lost when the theme is updated.
  • Custom code will not be executed when the theme is inactive.
  • No protection against erroneous PHP code.

Method 3 – Writing a simple site-specific plugin

As mentioned earlier, this is one of the best methods to insert custom code in WordPress.

In this method, we write our own single-file WordPress plugin which contains all the custom code.

This file can be edited directly from within the WordPress administration page.

You can already edit the source file of your existing plugin from the Plugins > Plugin editor page.

Just like this, we edit the plugin we wrote for our website. This offers the convenience of adding/managing code without any 3rd party plugins.

You can learn about writing your site-specific plugin in my article here.

Custom code added to WordPress using site-plugin

Since this is a plugin, the code you have inserted is active all the time even when you change themes.

Talking about the protection it offers when you insert erroneous code, it does offer some. Before we edit the plugin using the plugin editor, we must “deactivate” it, add/edit custom code and then enable it.

When we do this, WordPress will exit without activation when the plugin contains erroneous code. This gives some level of safety. But the user must remember to deactivate the plugin before making any changes.

If you want to disable a piece of code, you can simply “comment them“. The code is now inactive.

You can also upload your custom plugin directly within WordPress or use FTP. So this method offers a balance of all the features in the simplest way possible.

You can manage versions of your site-specific plugin using Github offline if you are into that.

This website also has a site-specific plugin using which I have added extra features.

Advantages

  • Full control of the site-specific plugin.
  • Safety against erroneous code being activated.
  • No extra overhead/bloat.
  • The convenience of editing code directly within the admin dashboard.

Disadvantages

  • Might be inconvenient for beginners when they are getting started.

Conclusion

In this article, we explored all the methods available to insert custom code into WordPress.

We also saw their advantages and disadvantages and explained why “Inserting custom PHP to WordPress using a site-specific plugin” is the best method.

If you have any comments, please share them in the comments section below.

Subscribe for updates

Get updates on the WordPress plugins, tips and tricks to enhance your WordPress experience. No spam. View newsletter

Add your comment No Comments so far