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_name
stringrequired- The name of the filter hook.
$callback
callable|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
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
Version | Description |
---|---|
2.5.0 | Introduced. |
Basic Example
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() .