WordPress is rightly famed for the vast array of plugins and themes it makes available to site owners. If you want to add a feature to your WordPress site, you will almost certainly find a plugin that does the job.
But, on occasion, you may find yourself in need of a minor tweak or piece of bespoke functionality that isn’t available as a plugin. The solution is to add a snippet of custom code to the site. WordPress is a PHP application and WordPress plugins and themes are written in the PHP programming language. As a WordPress hosting client, you have access to the same hooks and tools WordPress developers use.
You don’t even have to be a PHP expert to do this. There thousands of pre-made snippets around the web that you can adapt to your own purposes. Take care though, there are security implications to adding code to your site and badly written code can stop your WordPress site from working altogether. Make sure you know what a function does and that it is compatible with your version of WordPress before you add it to your site.
Once you have discovered the need for a function and written it from scratch or adapted a prewritten function, where should you put it?
There is a wrong way and a right way to do this. If you do it the “wrong” way, your function may work initially, but it is likely to stop working when you update your site.
How Not To Add Functions To WordPress
The two most common “bad” ways to add functions are editing an existing plugin or editing the functions.php file.
Don’t edit plugin files. If your snippet changes the functionality of a plugin, it might seem sensible to add the new code directly to the plugin. But, when you update the plugin, the files you have changed will be overwritten and your code will disappear.
The functions.php file is not a general purpose dumping ground for custom code. The functions.php file belongs to your theme. If the code you want to add is theme-specific, then functions.php is a good place to put it. But, when you switch themes, the new theme will not have the custom code. Avoid putting general-purpose custom code in functions.php.
The Right Way To Add Custom Functions
There are a couple of ways to add custom functions that will last beyond your next update or theme switch.
The Code Snippets Plugin
The Code Snippets plugin is designed for exactly this purpose. It provides a graphical interface for adding code snippets to a WordPress site. You can add as many snippets as you want, enable and disable them easily, and export them in a format that can be imported into other WordPress sites with the Code Snippets plugin.
Build A Custom Plugin
You might find the idea daunting, but it is not difficult to build a custom plugin that can be installed on a WordPress site alongside third-party plugins.
The basic structure of a minimal plugin looks like this:
- A folder with the same name as your plugin, e.g. my-plugin. This is not essential but it’s useful if you want to add more files in the future.
- A PHP file inside that folder called my-plugin.php
In the my-plugin.php file, add the following text:
<?php /* Plugin Name: Example Plugin */
That is essentially all you need to create a plugin, although it won’t do anything yet. To make it useful, you need to add your custom function to the PHP file and then upload the folder to the plugin directory of your WordPress site, usually wp-content/plugins/.
If you need to add new functions, you can simply overwrite the old version with your changes.
For more information about creating custom WordPress plugins, take a look at the Writing a Plugin guide in the ever useful WordPress Codex.