Using WordPress ‘has_action()’ PHP function

The has_action() WordPress PHP function checks if any action has been registered for a hook.

Usage

$result = has_action($hook_name, $callback);

Example:

if (has_action('init', 'custom_function') !== false) {
    echo 'The custom_function is hooked to the init action.';
}

Parameters

  • $hook_name (string) – The name of the action hook.
  • $callback (callable|string|array|false, optional) – The callback to check for. Default: false.

More information

See WordPress Developer Resources: has_action()

The function is an alias of has_filter(). When using the $callback argument, this function may return a non-boolean value that evaluates to false (e.g. 0), so use the === operator for testing the return value.

Examples

Check if the ‘init’ action has any hooks

The following code checks if any action has been registered for the ‘init’ hook and displays a message if it’s true.

if (has_action('init')) {
    echo 'The init action has hooks registered.';
}

Check if a specific function is hooked to the ‘init’ action

This code checks if the ‘custom_function’ is hooked to the ‘init’ action and displays a message if it’s true.

if (has_action('init', 'custom_function') !== false) {
    echo 'The custom_function is hooked to the init action.';
}

Check if a specific class method is hooked to the ‘wp_enqueue_scripts’ action

This example checks if the ‘enqueue_styles’ method of the ‘Theme_Styles’ class is hooked to the ‘wp_enqueue_scripts’ action and displays a message if it’s true.

if (has_action('wp_enqueue_scripts', array('Theme_Styles', 'enqueue_styles')) !== false) {
    echo 'The enqueue_styles method is hooked to the wp_enqueue_scripts action.';
}

This code checks if the ‘add_custom_js’ function is hooked to the ‘wp_footer’ action with a priority of 20 and displays a message if it’s true.

if (has_action('wp_footer', 'add_custom_js') === 20) {
    echo 'The add_custom_js function is hooked to the wp_footer action with a priority of 20.';
}

Unhook a specific function if it is hooked to the ‘wp_head’ action

This example checks if the ‘remove_version’ function is hooked to the ‘wp_head’ action and removes it if it’s true.

if (has_action('wp_head', 'remove_version') !== false) {
    remove_action('wp_head', 'remove_version');
}

### Example 1: Check if a custom action is registered
In this example, we check if the `custom_action_hook` has the `custom_function_name` action registered to it.

function custom_function_name() {
    // Do something
}

add_action('custom_action_hook', 'custom_function_name');

if (has_action('custom_action_hook', 'custom_function_name')) { echo 'Action is registered!'; } else { echo 'Action is not registered.'; }

### Example 2: Check if a default WordPress action is registered
In this example, we check if the `wp_head` action has the `wp_enqueue_scripts` function registered to it.
if (has_action('wp_head', 'wp_enqueue_scripts')) {
    echo 'wp_enqueue_scripts is registered in wp_head!';
} else {
    echo 'wp_enqueue_scripts is not registered in wp_head.';
}

Check if any action is registered to a hook

In this example, we check if any action has been registered to the custom_action_hook.

if (has_action('custom_action_hook')) {
    echo 'At least one action is registered!';
} else {
    echo 'No action is registered.';
}

Conditionally remove an action

In this example, we conditionally remove the custom_function_name action from the custom_action_hook if it is registered.

function custom_function_name() {
    // Do something
}

add_action('custom_action_hook', 'custom_function_name');

if (has_action('custom_action_hook', 'custom_function_name')) {
    remove_action('custom_action_hook', 'custom_function_name');
    echo 'Action has been removed.';
} else {
    echo 'Action was not registered.';
}

Register an action only if another action is registered

In this example, we register the second_custom_function action to the custom_action_hook only if the custom_function_name action is already registered.

function custom_function_name() {
    // Do something
}

function second_custom_function() {
    // Do something else
}

add_action('custom_action_hook', 'custom_function_name');

if (has_action('custom_action_hook', 'custom_function_name')) {
    add_action('custom_action_hook', 'second_custom_function');
    echo 'Both actions are now registered.';
} else {
    echo 'Only the first action is registered.';
}