has_filter()WP 2.5.0

Checks if any function is attached to the specified filter. You can specify the name of a specific function.

Used By: has_action()
1 time — 0.000043 sec (very fast) | 50000 times — 0.91 sec (very fast) | PHP 7.0.2, WP 4.4.2

No Hooks.

Returns

true|false|Int.

When the function being checked is specified:

  • Number (priority) — the function is attached to the hook.
  • false — the specified function is not attached to the hook.

When the function being checked is not specified:

  • true — the hook has at least one attached function.
  • false — the hook has no attached functions.

Usage

has_filter( $tag, $function_to_check );
$tag(string) (required)
The name of the filter to check.
$function_to_check(string/callback)
The name of the function to check for attachment to the filter.
Default: false

Examples

0

#1 Check if the filter has hooks the_content

if( has_filter( 'the_content' ) ){
	echo 'There is at least one function attached to `the_content` filter';
}
0

#2 Check if the_content filter has a specific function:

$priority = has_filter( 'the_content', 'my_function' );
if( $priority ){
	echo "`the_content` filter has my_function() attached with priority $priority";
}

Notes

  • Global. WP_Hook[]. $wp_filter Stores all of the filters and actions.

Changelog

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

has_filter() code WP 6.9.1

function has_filter( $hook_name, $callback = false, $priority = false ) {
	global $wp_filter;

	if ( ! isset( $wp_filter[ $hook_name ] ) ) {
		return false;
	}

	return $wp_filter[ $hook_name ]->has_filter( $hook_name, $callback, $priority );
}