current_filter()WP 2.5.0

Retrieve the name of the current filter or action.

1 time — 0.00001 sec (speed of light) | 50000 times — 0.01 sec (speed of light) | PHP 7.1.11, WP 4.9.5

No Hooks.

Return

String. Hook name of the current filter.

Usage

current_filter();

Examples

0

#1 Dynamically changing the email header

An interesting example that shows how you can dynamically change the header of an email (who the email is from), depending on which filter the same function (filter_system_from_mail) is triggered from.

add_filter( 'wp_mail_from',      'filter_system_from_mail' );
add_filter( 'wp_mail_from_name', 'filter_system_from_mail' );

function filter_system_from_mail(){

	$opt = 'wp_mail_from' == current_filter() ? 'admin_email' : 'blogname';

	return get_option( $opt );
}
0

#2 Usage Example

Get the current filter name:

add_filter( 'the_content', 'wpdocs_my_filter' );

function wpdocs_my_filter() {
	echo current_filter(); // 'the_content'
}

Get the current action name:

add_action( 'init', 'wpdocs_my_init_function' );

function wpdocs_my_init_function() {
	echo current_filter(); // 'init'
}

Notes

  • Global. String[]. $wp_current_filter Stores the list of current filters with the current one last

Changelog

Since 2.5.0 Introduced.

current_filter() code WP 6.5.3

function current_filter() {
	global $wp_current_filter;

	return end( $wp_current_filter );
}