doing_filter()
Checks whether the specified hook (filter, action) is currently being processed.
Allows determining the hook that is being executed at the current moment (the moment this function is called). The function will not output anything if the last hook is currently being executed.
The function can “store” several hook names for checking at once, for example when one hook is called from another. See examples.
Аналогичные функции:
- current_filter() — gets the name of the current action or filter.
- current_action() — copy of current_filter().
- doing_filter() — checks whether the specified hook is currently being processed.
- doing_action() — copy of doing_filter().
- did_action() — returns an int representing how many times the specified hook has fired.
- did_filter() — Since WP 6.1. Returns an int representing how many times the specified hook has fired.
No Hooks.
Returns
true|false. true if the specified hook matches the currently executing one, false otherwise.
Usage
if( doing_filter( $filter_name ) ){
// ...
}
- $filter(string/null)
- The name of the hook to check. If you specify null (do not specify anything) then the function will check whether any hook is currently being executed.
Default: null
Examples
#1 Demo
add_action( 'wp_head', function(){
doing_filter( 'wp_head' ); //> true
var_dump( doing_filter('wp_head') );
die;
} );
As a Result: When the wp_head action is triggered, WP will be interrupted and the bool(true) will be displayed.
#2 Different actions in different hooks that have the same handler function
The example shows how you can dynamically change the email header depending on which filter is triggered by the same function.
add_filter( 'wp_mail_from', 'filter_system_from_mail' );
add_filter( 'wp_mail_from_name', 'filter_system_from_mail' );
function filter_system_from_mail(){
if( doing_filter('wp_mail_from') )
$opt_name = 'admin_email';
else
$opt_name = 'blogname';
return get_option( $opt_name );
} #3 Checking the parent (check) hook
doing_filter() is able to check the "chain" hook being executed.
Suppose a function is attached to hook foo_filter and it calls hook bar_filter. Inside the function attached to the hook bar_filter you can check if the hook foo_filter is currently running.
add_filter( 'foo_filter', 'foo_filter_function' );
add_filter( 'bar_filter', 'bar_filter_function' );
echo apply_filters( 'foo_filter', 'Hello' ); //> Hello world!
function foo_filter_function( $text ){
return apply_filters( 'bar_filter', $text );
}
function bar_filter_function( $text ){
// Let's check the execution of two hooks at once:
// - the current one (bar_filter) to which the function is attached
// - and the parent one (foo_filter) to which the function that
// caused the current 'bar_filter' hook is attached.
// I.e. at the moment 2 hooks are executed at once:
// 'foo_filter' and 'bar_filter' and the function.
// doing_filter() can check any of the currently running hooks
if( doing_filter('foo_filter') && doing_filter('bar_filter') ){
return "$text world!";
}
return 'Noooop!';
}
As you can see from the code, in the function that is attached to the hook bar_filter both conditions are triggered: doing_filter('foo_filter') and doing_filter('bar_filter'). Which means that the two hooks are currently running at once.
Notes
- See: current_filter()
- See: did_filter()
- Global. String[].
$wp_current_filterCurrent filter.
Changelog
| Since 3.9.0 | Introduced. |
doing_filter() doing filter code WP 7.0
function doing_filter( $hook_name = null ) {
global $wp_current_filter;
if ( null === $hook_name ) {
return ! empty( $wp_current_filter );
}
return in_array( $hook_name, $wp_current_filter, true );
}