Home WordPress Hooks Functions apply_filters() WordPress apply_filters() Function Definition In WordPress, apply_filters() takes a value and passes it through every filter added to a hook. It then returns the final value.
Think of a conveyor belt. You put a value on the belt. Each function added with add_filter() may change it. What comes out at the end is the result. If nobody added a filter, you get your original value back. WordPress uses it for post content, titles, and many settings. You can use it in your own theme or plugin to let others change your values.
Syntax Give it the hook name and the value. Save the result in a variable.
<?php $greeting = apply_filters( 'my_greeting', 'Hello' ); ?> Call What it does apply_filters( 'my_greeting', 'Hello' )Filter the text Hello on your own hook. apply_filters( 'my_price', $price, $id )Filter $price and also send $id as extra information. apply_filters( 'the_content', $text )Run text through the same filters WordPress uses for post content.
The result is returned, so assign it to a variable or print it. Calling it alone does nothing useful.
Full declaration apply_filters( string $hook_name, mixed $value, mixed ...$args ): mixed Parameters Parameter Description $hook_namestring. The name of the filter hook. For your own hook, use a unique name with a prefix, such as ctf_greeting. $valuemixed. The value to filter. It can be text, a number, or an array. This value goes to the first filter. ...$argsmixed. Optional extra values sent to every filter. Filters can read them, but only $value is changed.
Return Value Return Value Description mixed The filtered value. If no filter is attached, it is the same as the value you passed in.
Important
Use the returned value. apply_filters( 'my_hook', $value ); on its own does not change $value. Write $value = apply_filters( 'my_hook', $value );. Also, every filter must return a value, or the result will be empty.
WordPress Examples Three short examples. Each one shows the code and what it prints or does.
1. Make your own filter hook 2. Send extra values to filters 3. Use a WordPress filter yourself Example 1 — Make your own filter hook Your function gives a default text. Anyone can change it with a filter.
function ctf_get_greeting() {
return apply_filters( 'ctf_greeting', 'Hello' );
}
function ctf_shout( $text ) {
return strtoupper( $text ) . '!';
}
add_filter( 'ctf_greeting', 'ctf_shout' );
echo ctf_get_greeting(); How It Works apply_filters() sends Hello through the ctf_greeting hook.ctf_shout() is attached to that hook. It makes the text upper case and adds an exclamation mark.The changed value is returned. Remove the filter and the output would be Hello. Example 2 — Send extra values to filters Extra arguments give filters more information, but they are not changed.
function ctf_discount( $price, $product ) {
if ( 'book' === $product ) {
return $price - 10;
}
return $price;
}
add_filter( 'ctf_price', 'ctf_discount', 10, 2 );
$final = apply_filters( 'ctf_price', 100, 'book' );
echo 'Final price: ' . $final; How It Works The value to change is 100. The word book is an extra argument. The filter asks for 2 values with 10, 2, so it can check the product name. For a book it returns 90. For any other product it returns the original price. Example 3 — Use a WordPress filter yourself You can run text through a core filter such as the_content.
$text = "Hello world.\n\nSecond line.";
echo apply_filters( 'the_content', $text ); <p>Hello world.</p>
<p>Second line.</p> How It Works The plain text has a blank line between two sentences. WordPress attaches filters such as wpautop to the_content. Running the text through apply_filters() wraps each paragraph in <p> tags. Frequently Asked Questions What does apply_filters() do? It sends a value through all functions added to a filter hook and returns the final value.
What is the difference between apply_filters() and add_filter()? apply_filters() is the place where the value is filtered. add_filter() attaches a function that changes the value at that place.
What if no filter is added to the hook? apply_filters() returns the original value, unchanged. So it is safe to add hooks to your own code.
Do I have to use the return value? Yes. Save it in a variable or print it. The original variable is not changed unless you assign the result.
How should I name my own filter hook? Use a unique prefix, such as ctf_greeting or myplugin_price, so it does not clash with WordPress or other plugins.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion