has_action()
Checks if a function has been registered for the event hook.
Uses: has_filter()
1 time — 0.000016 sec (very fast) | 50000 times — 0.03 sec (speed of light)
No Hooks.
Returns
true|false|Int.
When the function being checked is specified:
Number(priority) — if the function to check is found on the hook.false— If the specified function is not attached to the hook.
When the function being checked is not specified:
true— if the hook has at least one attached function.false— If the hook has no attached functions at all.
Usage
$has = has_action( $tag, $function_to_check );
- $tag(string) (required)
- The name of the hook (action) whose functions need to be checked.
- $function_to_check(string/callback)
- The name of the function to check if it is attached to the hook specified in the first parameter.
Default: false
Examples
#1 Check if the event init has hooks:
if( has_action( 'init' ) ){
echo 'The `init` action has at least one function attached';
}
#2 Check if the event init has a specific function:
$priority = has_action( 'init', 'my_function' );
if( $priority ){
echo "The init hook has my_function() attached with priority $priority";
}
Notes
- See: has_filter() This function is an alias of has_filter().
Changelog
| Since 2.5.0 | Introduced. |
| Since 6.9.0 | Added the $priority parameter. |
has_action() has action code WP 6.9.1
function has_action( $hook_name, $callback = false, $priority = false ) {
return has_filter( $hook_name, $callback, $priority );
}