How to create a WordPress plugin for your custom actions and filters

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.

  1. Using the sample plugin header below, copy to a new text file and save as a PHP file. For example gg-custom-functions.php
  2. <?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
    */
  3. Add your custom functions below the closing */
  4. TIP: make sure that you do not have the same functions anywhere else – such as the themes function.php file.
  5. ZIP the file, so you have something like this sample plugin: itsg-custom-functions.zip
  6. Now open up the WordPress adminisation console
  7. Open the Plugins page and click on ‘Add Plugin’
  8. Now click on the ‘Upload Plugin’ button and upload the ZIP file
  9. Activate the plugin
  10. You can now maintain all your custom functions from the plugin instead of your theme’s functions.php file.

Article Downloads

TIP: You may need to right-click and select 'save link as'.