Using WordPress ‘doing_action()’ PHP function

The doing_action() WordPress PHP function is used to determine whether a specified action hook is currently being processed. It provides an effective way to check if any action is currently running, even if it’s not the most recent one. It’s especially helpful in scenarios where multiple hooks are called from within hook callbacks.

Usage

Let’s consider an example where we want to check if the ‘save_post’ action is currently being processed.

if ( doing_action( 'save_post' ) ) {
    // Perform task if 'save_post' is being processed.
}

Parameters

  • $hook_name (string|null): Optional parameter. This is the action hook to check. If not provided or set to null, the function will check if any action is currently being run.

More information

See WordPress Developer Resources: doing_action()

This function was implemented in WordPress 3.9.0 and, as of the last update, it is not deprecated.

Examples

Checking if ‘save_post’ action is being processed

This code will allow you to perform a certain task if the ‘save_post’ action is currently being processed.

if ( doing_action( 'save_post' ) ) {
    // Perform task
}

Checking if any action is being processed

This code will check if any action is currently being executed.

if ( doing_action() ) {
    // Perform task
}

Checking ‘init’ action

The following example checks whether the ‘init’ action hook is currently being processed.

if ( doing_action( 'init' ) ) {
    // Perform task
}

Checking ‘admin_init’ action

This code checks if the ‘admin_init’ action is being processed.

if ( doing_action( 'admin_init' ) ) {
    // Perform task
}

Checking ‘wp_head’ action

Here’s how to check if the ‘wp_head’ action is currently being processed.

if ( doing_action( 'wp_head' ) ) {
    // Perform task
}