WordPress apply_filters() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Filter hook

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.

WordPress
<?php $greeting = apply_filters( 'my_greeting', 'Hello' ); ?>
CallWhat 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
WordPress
apply_filters( string $hook_name, mixed $value, mixed ...$args ): mixed

Parameters

ParameterDescription
$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 ValueDescription
mixedThe filtered value. If no filter is attached, it is the same as the value you passed in.

WordPress Examples

Three short examples. Each one shows the code and what it prints or does.

Example 1 — Make your own filter hook

Your function gives a default text. Anyone can change it with a filter.

WordPress
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

  1. apply_filters() sends Hello through the ctf_greeting hook.
  2. ctf_shout() is attached to that hook. It makes the text upper case and adds an exclamation mark.
  3. 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.

WordPress
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

  1. The value to change is 100. The word book is an extra argument.
  2. The filter asks for 2 values with 10, 2, so it can check the product name.
  3. 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.

WordPress
$text = "Hello world.\n\nSecond line.";
echo apply_filters( 'the_content', $text );

How It Works

  1. The plain text has a blank line between two sentences.
  2. WordPress attaches filters such as wpautop to the_content.
  3. Running the text through apply_filters() wraps each paragraph in <p> tags.

Frequently Asked Questions

It sends a value through all functions added to a filter hook and returns the final value.
apply_filters() is the place where the value is filtered. add_filter() attaches a function that changes the value at that place.
apply_filters() returns the original value, unchanged. So it is safe to add hooks to your own code.
Yes. Save it in a variable or print it. The original variable is not changed unless you assign the result.
Use a unique prefix, such as ctf_greeting or myplugin_price, so it does not clash with WordPress or other plugins.

Next: do_action()

Fire an action hook so every function added to it runs.

do_action() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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