has_filter( string $tag, callable|bool $function_to_check = false )

Check if any filter has been registered for a hook.


Description Description


Parameters Parameters

$tag

(string) (Required) The name of the filter hook.

$function_to_check

(callable|bool) (Optional) The callback to check for.

Default value: false


Top ↑

Return Return

(false|int) If $function_to_check 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. When using the $function_to_check 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.


Top ↑

Source Source

File: wp-includes/plugin.php

function has_filter( $tag, $function_to_check = false ) {
	global $wp_filter;

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

	return $wp_filter[ $tag ]->has_filter( $tag, $function_to_check );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 2 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.