doing_filter()
Retrieve the name of a filter currently being processed.
The function current_filter() only returns the most recent filter being executed. did_filter() returns the number of times a filter has been applied during the current request.
This function allows detection for any filter currently being executed (regardless of whether it's the most recent filter to fire, in the case of hooks called from hook callbacks) to be verified.
No Hooks.
Return
true|false
. Whether the filter is currently in the stack.
Usage
doing_filter( $hook_name );
- $hook_name(string|null)
- Filter hook to check.
Default: null, which checks if any filter is currently being run
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_filter Current filter.
Changelog
Since 3.9.0 | Introduced. |
doing_filter() doing filter code WP 6.6.2
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 ); }