has_action()WP 2.5.0

Checks if a function has been registered for the event hook.

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

0

#1 Check if the event init has hooks:

if( has_action( 'init' ) ){
	echo 'The `init` action has at least one function attached';
}
0

#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

Changelog

Since 2.5.0 Introduced.
Since 6.9.0 Added the $priority parameter.

has_action() code WP 6.9.1

function has_action( $hook_name, $callback = false, $priority = false ) {
	return has_filter( $hook_name, $callback, $priority );
}