Typically WordPress plugins are for a single purpose – such as a tool, and any custom functions you need are added to your active theme’s functions.php file.
The issue with this is that each time the theme is updated you lose the changes made to the functions.php file.
One way around this is to create a child theme – but it still has it’s own limitations, such as the plugins_loaded action hook doesnt fire because it needs to be called before the theme has loaded.
The answer to this is to create a simple custom plugin that can contain all your custom functions. This way you can update your theme without worrying about loosing any of your customisations.
There are other advantages to this – it’s easier to maintain as you can use the plugin version number to create your own version control, and easier to debug – if you have any issues you can just disable the plugin to see if the issues are caused by your customisations.
Here’s what you need to do.
- Using the sample plugin header below, copy to a new text file and save as a PHP file. For example itsg-custom-functions.php
<?php /* Plugin Name: itsupportguides.com custom functions Description: Contains custom functions Version: 1.0.0 Author: IT Support Guides Author URI: itsupportguides.com */
- Add your custom functions below the closing */
- TIP: make sure that you do not have the same functions anywhere else – such as the themes function.php file.
- ZIP the file up, so you have something like this sample plugin: itsg-custom-functions.zip
- Now open up the WordPress administration console
- Open the Plugins page and click on ‘Add Plugin’
- Now click on the ‘Upload Plugin’ button and upload the ZIP’ed plugin file
- Activate the plugin
- You can now maintain all your custom functions from the plugin instead of your theme’s functions.php file.
Stuck? Have a look at my sample plugin: itsg-custom-functions.zip