skip to Main Content

Run a WordPress hook

Warning: This action lets you run custom PHP code on your site.  Custom PHP code can break your site, rendering it unusable and inaccessible.  You should only use this action if you are familiar with writing and debugging PHP code.  Always test on a staging environment before deploying to your live site.

The Run a WordPress hook action can call any existing WordPress action or any custom-defined plugin or theme action.

Imagine creating a recipe that executes your custom-defined action–it’s like telling Automator “execute this code” when “these other things” happen. This action is very powerful and can be very useful to developers or site owners who want to run a custom function as a recipe.

Let’s take a look at setting this up!

Adding a new Automator action hook

First, make sure you’re running Automator and Automator Pro version 3.0 or later.

Create a recipe, then, click the Add Action button and choose Automator Core.  Then click Run a WordPress Hook:

This is what you’ll see next:

The Hook name is the name of the WordPress action hook that you want the Automator Action to run, e.g. my_custom_action_hook. Click here to learn more about WordPress action hooks.

Pass variables fields will contain the data that you want to pass into the callback function of the specified hook (e.g. my_custom_action_hook).

Each particular action hook contains a different number of arguments. You may skip this field if the hook you’re using does not accept any arguments. Otherwise, you may pass a custom-defined values to the action hook argument by clicking the Add a variable button and entering the data you want to pass into the function.

You may add any number of values or remove them as necessary. The order of the arguments passed to the callback function will be based on the order you define in the Pass variables field (e.g. from top to bottom):

In this example, we will be adding just one field value since we be passing only one argument into our callback function. Don’t forget to save the new Automator Action we’ve just set up by clicking Save.

Go ahead and add new Triggers. In this example we’ll use the WordPress Trigger A user views a page.

Switch the status of the Recipe to live. At this point, you can try completing the recipe triggers.  You’ll probably notice that nothing happens; this is because we have not yet defined an action hook called my_custom_action_hook.

Writing the callback function for the custom hook

Let’s set up our new action hook inside our child theme.  In the callback function, we will redirect the user to the URL specified in the Pass variables field. In this example, we’ll set the URL to:

https://sitename.com/?my_custom_url_param=yes (replace https://sitename.com with your own domain)

Here’s how we’ll add the function to our child theme:

  1. Open your favorite text editor. You may use Sublime text, PHP Storm, Notepad++, Visual Studio Code, or any editor of your choice.
  2. Locate your theme or child theme’s functions.php file.
  3. Paste in the following code:
if ( ! function_exists('my_custom_action_hook_cb_function') ) {
    add_action('my_custom_action_hook', 'my_custom_action_hook_cb_function', 10, 1);
    function my_custom_action_hook_cb_function( $url ){
        wp_safe_redirect( $url );
        exit;
    }
}
  1. Save the file and visit the page you use earlier in trigger. You should be redirected to https://sitename.com/?my_custom_url_param=yes

Here’s how the code works:

  1. add_action(‘my_custom_action_hook’, ‘my_custom_action_hook_cb_function’, 10, 1) – The first argument here is the my_custom_action_hook which is what you specified in the Hook name field.
  2. The second argument my_custom_action_hook_cb_function is the function that will be run when the action gets executed.
  3. The 3rd argument is 10 which the loading priority of that action.  Typically, this can be left at 10, unless you’re hooking a number of functions to this action and want them to run in a specific order.
  4. And lastly, the fourth argument which is 1 which must be equal to the number of variables that you’ve defined in the Pass variables field.

Error checking

You should check for the following things if you encountered any errors while adding a custom action hook:

  1. Make sure that the Recipe is set to Live.
  2. Check for any spelling errors, etc. Check that the hook name in add_action matches with the one that you entered in the Hook name field.
  3. The number of arguments provided in the Pass variable field must match the number of arguments that you passed in your callback function.
  4. Check your PHP code if you run into any errors or warnings.

That’s it!  We hope this makes creating custom code for your WordPress site much easier, faster and more fun.

Back To Top