wp-includes/plugin.php:174Pass a value through every callback attached to a filter hook with apply_filters() and get the filtered result back. Calling it with a new hook name is how you create a filter in your own plugin or theme; any arguments after $value are handed to callbacks as extra context.
Calls the callback functions that have been added to a filter hook.
example_filter above.
*
* - 'example_filter' is the filter hook.
* - 'filter me' is the value being filtered.
* - $arg1 and $arg2 are the additional arguments passed to the callback.
$value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );$hook_namestring$valuemixed$argsmixedmixedEvery example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Wrap an internal value in apply_filters() so other plugins and themes can override it.
function myshop_get_currency_symbol() {
$symbol = '$';
// Other code can change this via add_filter( 'myshop_currency_symbol', ... ).
return apply_filters( 'myshop_currency_symbol', $symbol );
}
echo esc_html( myshop_get_currency_symbol() );The result is whatever the last callback returns, so never assume the filtered value still has the original type; validate before using it.
Send extra arguments after $value so callbacks can make decisions based on context like a post ID.
function myplugin_card_excerpt( $post_id ) {
$excerpt = wp_trim_words( get_the_excerpt( $post_id ), 20 );
// Callbacks receive the excerpt plus the post ID as context.
return apply_filters( 'myplugin_card_excerpt', $excerpt, $post_id );
}
echo esc_html( myplugin_card_excerpt( 42 ) );Only the first argument ($value) is filtered and returned; the extra arguments are read-only context, and callbacks only receive them if their add_filter() call sets $accepted_args high enough.
function apply_filters( $hook_name, $value, ...$args ) { global $wp_filter, $wp_filters, $wp_current_filter; if ( ! isset( $wp_filters[ $hook_name ] ) ) { $wp_filters[ $hook_name ] = 1; } else { ++$wp_filters[ $hook_name ]; } // Do 'all' actions first. if ( isset( $wp_filter['all'] ) ) { $wp_current_filter[] = $hook_name; $all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection _wp_call_all_hook( $all_args ); } if ( ! isset( $wp_filter[ $hook_name ] ) ) { if ( isset( $wp_filter['all'] ) ) { array_pop( $wp_current_filter ); } return $value; } if ( ! isset( $wp_filter['all'] ) ) { $wp_current_filter[] = $hook_name; } // Pass the value to WP_Hook. array_unshift( $args, $value ); $filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args ); array_pop( $wp_current_filter ); return $filtered;}Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
src/wp-includes/plugin.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.