The has_action() WordPress function

When writing a WordPress plugin or theme, you may need to check if an action hook exists before running additional code. In this article, we will show you the has_action WordPress function as well as how to use it.

Usage

<?php has_action( $tag, $function_to_check ) ?>

Parameters

The has_action function includes 2 parameters, on of them being required and the other optional. The following are the parameters that can be defined:

$tag

This parameter is simply the name of the action hook that you want to check for actions on. This is a required parameter as without it, the has_action function would not know what hook to check for.

$function_to_check

The $function_to_check is an optional parameter that can be used to check if a custom function has been hooked into the action. By default, it is set to false but if the custom function is defined here, it will return the priority of the function. If the function does not exist, it will return false.

Code Example

Determine if an action exists

if ( has_action(‘my_hook’, ‘hooked_action’) ) {
// do something
}

In the above example, we are simply checking to see if the hook my_hook has an action registered for it. If it does, additional code will be executed.

Leave a Reply