The current_filter() WordPress PHP function retrieves the name of the current filter hook.
Usage
To use the function, simply call it within your custom function or action. See this example:
function my_content_filter() { echo current_filter(); // Outputs: 'the_content' } add_filter( 'the_content', 'my_content_filter' );
In this example, current_filter()
is used to retrieve and echo the name of the current filter hook which is ‘the_content’.
Parameters
This function does not take any parameters.
More information
See WordPress Developer Resources: current_filter()
Examples
Display Current Filter within a Content Filter
function display_current_filter() { echo current_filter(); // Outputs: 'the_content' } add_filter( 'the_content', 'display_current_filter' );
This code displays the name of the current filter hook within a content filter. It should output ‘the_content’.
Display Current Filter within an Init Action
function display_current_action() { echo current_filter(); // Outputs: 'init' } add_action( 'init', 'display_current_action' );
This code displays the name of the current action hook within an init action. It should output ‘init’.
Check Current Filter Name in a Custom Filter
function check_current_filter() { if (current_filter() == 'the_content') { echo 'Content filter is currently active.'; } } add_filter( 'the_content', 'check_current_filter' );
This code checks if the current filter is ‘the_content’, and if so, it echoes a message.
Use Current Filter in a Conditional Statement
function condition_based_on_filter() { if (current_filter() == 'the_content') { // Do something when the content filter is running } } add_filter( 'the_content', 'condition_based_on_filter' );
This code performs an action only when the current filter is ‘the_content’.
Log Current Filter Name
function log_current_filter() { error_log(current_filter()); } add_filter( 'all', 'log_current_filter' );
This code logs the name of each filter as it runs. It uses the ‘all’ hook, which is fired for all actions and filters.