has_action()
Check if any action has been registered for a hook.
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.
Uses: has_filter()
1 time — 0.000016 sec (very fast) | 50000 times — 0.03 sec (speed of light)
No Hooks.
Return
true|false|Int
. If $callback is omitted, returns boolean for whether the hook has anything registered. When checking a specific function, the priority of that hook is returned, or false if the function is not attached.
Usage
has_action( $hook_name, $callback );
- $hook_name(string) (required)
- The name of the action hook.
- $callback(callable|string|array|false)
- The callback to check for. This function can be called unconditionally to speculatively check a callback that may or may not exist.
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. |
has_action() has action code WP 6.8
function has_action( $hook_name, $callback = false ) { return has_filter( $hook_name, $callback ); }