Sometimes you might need to add a PHP function in your WordPress site, this could be for many different reasons, quite often to insert something into a ‘hook’ in your theme or to create a ‘filter’ to change an existing function.
There are three easy ways that you can do this, two that are super simple and one that is slightly more involved. The great news is that any of them will work just fine.
Use a child theme
If all you want to do is add one or two PHP functions then creating a child them might seem a bit like overkill, but if you are planning on changing CSS and maybe also theme template files then it’s a sensible way to go.
If you already have a child theme then you can just add the functions into your functions.php file. If you want to create a new child theme then you can find the full guide of how to do it on wordpress.org.
Code Snippets plugin
The Code Snippets plugin lets you add PHP snippets from within the WordPress admin page. You can easily edit them, activate/deactivate them, and also annotate them with notes to yourself.
There are probably a lot of other plugins floating around that do a similar thing, but this is the one that we recommend using, it’s easy, simple, fast, and well maintained.
Install the plugin by going to Plugins in the left menu, then Add New and then search for Code Snippets. Once it’s installed everything is pretty self-explanatory.
Create your own plugin
The third option is to create your own plugin to hold all of your PHP functions. The advantage of this method over using a child theme is that your functions remain if you change theme.
Creating a plugin is not difficult but it is a bit more involved than either of the other two options above. We will be putting together a tutorial on exactly how to do it very soon.