Often people are told to add custom functions to the 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 doesn’t 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 gg-custom-functions.php
<?php /* Plugin Name: Gravity Geek custom functions Description: Contains custom actions and filters Version: 1.0.0 Author: Gravity Geek Author URI: https://www.gravitygeek.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, so you have something like this sample plugin: itsg-custom-functions.zip
- Now open up the WordPress adminisation console
- Open the Plugins page and click on ‘Add Plugin’
- Now click on the ‘Upload Plugin’ button and upload the ZIP file
- Activate the plugin
- You can now maintain all your custom functions from the plugin instead of your theme’s functions.php file.