doing_filter()WP 3.9.0

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.

Аналогичные функции:

Used By: doing_action()
1 time — 0.000012 sec (very fast) | 50000 times — 0.01 sec (speed of light) | PHP 7.0.8, WP 4.5.3

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

0

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

0

#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 );
}
0

#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

Changelog

Since 3.9.0 Introduced.

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 );
}