has_filter( string $hook_name, callable|string|array|false $callback = false ): bool|int

Checks if any filter has been registered for a hook.

Description

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.

Parameters

$hook_namestringrequired
The name of the filter hook.
$callbackcallable|string|array|falseoptional
The callback to check for.
This function can be called unconditionally to speculatively check a callback that may or may not exist.

Default:false

Return

bool|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.

Source

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

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

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

Changelog

VersionDescription
2.5.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    add_filter() calls the same _wp_filter_build_unique_id() function and re-assigns the method/function parameter to the index array.

    It is very likely that calling add_filter() with the same parameter list is faster than first checking if the method/function is already registered with has_filter( , ) before adding it with add_filter( , );

    I am guessing the best use-case for has_filter() is to check if a filter has ANY registered methods versus checking that a specific method exists prior to re-registering it with add_filter() .

You must log in before being able to contribute a note or feedback.