Skip to content

Run a WordPress hook

The Run a WordPress hook action, in the Run Code integration, fires a WordPress action hook when a recipe runs: an existing WordPress hook, a hook from a plugin or theme, or a custom hook you define yourself. Every function attached to that hook then runs with the variables you pass, which is like telling Uncanny Automator “run this code” when “these other things” happen. The action is for developers and site owners who want to run their own functions from a recipe, and it requires Uncanny Automator Pro.

Important: This action runs custom PHP code on your site. Custom PHP code can break your site and make it unusable or inaccessible, so use the action only if you are comfortable writing and debugging PHP, and always test on a staging site before you use it on a live one.

Adding the action

  1. Open the recipe at Automator > Recipes, or create one from Automator > Add new recipe, and add its trigger; see Creating a recipe. This example uses the WordPress trigger A user views a page.
  2. Click the + at the end of the flow. In the picker, type Run Code in the search box and select Run Code, then Run a WordPress hook.

The picker on its Action step for Run Code, listing the three Pro items Call a custom function/method, Run JavaScript code and Run a WordPress hook

  1. In the Set up step, enter the Hook name: the WordPress action hook the recipe should fire, for example my_custom_action_hook. As the hint under the field says, it can be an existing hook or a custom one. See Actions in the WordPress Plugin Handbook to learn more about action hooks.
  2. Under Pass variables, click Add a variable once for each argument the hook’s callback function accepts, and enter its Value. Use the ∗ button to insert tokens where a value should come from the trigger. Variables are passed in the order listed, top to bottom, and null, [] and array() are passed as null and empty arrays. Skip this field if the hook takes no arguments. This example passes one variable: the URL to send the user to.

The Set up step of Run a WordPress hook with my_custom_action_hook as the Hook name, one URL under Pass variables, and the Cancel and Save buttons

  1. Click Save. This action has no Test step, so Save finishes it.
  2. Switch Recipe on in the top bar.

If you complete the trigger now, nothing happens yet: no function is attached to my_custom_action_hook. The next section adds one.

Writing the callback function

This example adds the callback function to your child theme. It redirects the user to the URL passed in Pass variables, for example https://sitename.com/?my_custom_url_param=yes (replace sitename.com with your own domain).

  1. Open your theme or child theme’s functions.php file in a text editor.
  2. Paste in the following code and save the file:
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. Visit the page you chose in the trigger. You should be redirected to the URL you passed.

Note: For a redirect on its own, the Redirect integration’s action does the same without code; see Working with redirects. The example is here because it is easy to see working.

How the code works

The add_action( 'my_custom_action_hook', 'my_custom_action_hook_cb_function', 10, 1 ) line attaches your function to the hook. Its four arguments are:

  1. my_custom_action_hook — the hook name. It must match the Hook name field in the action exactly.
  2. my_custom_action_hook_cb_function — the function that runs when the hook fires.
  3. 10 — the priority. Leave it at 10 unless several functions are attached to the hook and they must run in a specific order.
  4. 1 — the number of arguments the function accepts. It must equal the number of variables in Pass variables.

Troubleshooting

If the hook does not do what you expect, check these in order:

  1. Recipe on is switched on, and the action and trigger are On.
  2. The hook name in add_action matches the Hook name field exactly, with no spelling differences.
  3. The number of variables in Pass variables matches the number of arguments your callback function accepts.
  4. Your code runs without PHP errors or warnings.

The action fires the hook and records itself as completed whether or not any function is attached to it, so a completed entry on the Action tab of Automator > Logs does not prove your code ran. If nothing happens, check that the file containing your add_action call is loaded on the site.

Related: Call a custom function/method, Managing actions, Managing tokens

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top