Run a WordPress hook

Run a WordPress hook action lets you run any WordPress hook registered in your WordPress and pass the variables of your choice to that hook. WordPress hooks docs.

When you add a new action, you can find the run a WordPress hook action under the AutomatorWP integration.

Next, click on the “a WordPress hook” button and the action configuration will be displayed like this:

Simply, you need to enter the hook name to call and the variables you want to pass to this hook and AutomatorWP will call it at the moment the automation gets completed!

In addition, you will have all information in logs to meet if the hook has been executed correctly or if there was any error like incorrect parameters or an issue inside the hook’s code.

Note: If you do not know how to add your custom code to your WordPress, we recommend the plugin Code Snippets or a similar one that lets you add code to your site without edit your site files.

Example: Run a custom hook to execute a custom function

Lets to create an automation to update a user meta with some conditional logic. For this one we will use the trigger “User submits a form” from Gravity Forms to get the field value the user entered in the form and update a user meta with the field value only if the field is greater than 100.

Note: If the proposal of your hook is only execute a custom function, you have a more easy way through the Call a function action.

The automation setup:

The code:

add_action( 'my_custom_hook', 'function_for_my_custom_hook', 10, 2 );
function function_for_my_custom_hook( $user_id, $form_field_value ) {

    // Ensure that user ID and the form field value are integer numbers
    $user_id = (int) $user_id;
    $form_field_value = (int) $form_field_value;

    // Here you can add a conditional logic of your choice like:
    // - Apply some calc over the field value
    // - Or check if user has a specific role
    // - Or anything you want

    // Check if the form field value is lower than 100, then do not update the meta
    if( $form_field_value < 100 ) return;

    // Get the meta value
    $field_stored = (int) get_user_meta( $user_id, '_custom_field', true );

    // Update the user meta adding the field value to the already stored one
    update_user_meta( $user_id, '_custom_field', $field_stored + $form_field_value );

}

Example: Run an existent hook

WooCommerce has functionalities that are executed when a order status changes, like add notes in the order or send an email to the customer to notify about this change.

In this example, lets to force to run the WooCommerce hook “woocommerce_order_status_pending_to_completed_notification” to force to resend the notifications when a order changes from “Pending” to “Completed” status.

Note: For the example we will pass a plain order ID, but remember that you have the tags to pass dynamic values to any field.

The action setup: