WordPress add_filter() Function

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

Definition

In WordPress, add_filter() says: “When this value goes by, let me change it.” A hook that passes a value along so you can change it is called a filter.

WordPress calls apply_filters() on values such as post content, titles, and excerpt length. Each function you add with add_filter() receives the value, may change it, and must return it. The final value is what WordPress uses.

Syntax

Give it the hook name and the name of your function. The function must return a value.

WordPress
<?php add_filter( 'the_content', 'my_filter' ); ?>
CallWhat it does
add_filter( 'the_content', 'my_note' )Change the post content before it is shown.
add_filter( 'excerpt_length', 'my_length' )Change the number of words in an excerpt.
add_filter( 'the_title', 'my_title', 10, 2 )Let my_title() receive the title and the post ID.

The first value your function receives is always the value to change. Change it, then return it.

Full declaration
WordPress
add_filter( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 ): true

Parameters

ParameterDescription
$hook_namestring. The name of the filter hook, for example the_content, the_title, or excerpt_length.
$callbackcallable. The function that receives the value and returns the new value.
$priorityint. Order of running. Lower numbers run earlier. Default is 10. Each filter gets the value left by the one before it.
$accepted_argsint. How many values from the hook your function can receive. Default is 1.

Return Value

Return ValueDescription
trueAlways returns true. This is the result of adding the filter, not the filtered value.

WordPress Examples

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

Example 1 — Add a note after the post

Change the post content with the the_content filter.

WordPress
function ctf_add_note( $content ) {
  if ( is_single() && in_the_loop() && is_main_query() ) {
    $content .= '<p><em>Thanks for reading!</em></p>';
  }
  return $content;
}
add_filter( 'the_content', 'ctf_add_note' );

How It Works

  1. WordPress passes the post content into ctf_add_note() as $content.
  2. The function adds a short note to the end, but only on a single post in the main loop.
  3. return $content; sends the new content back. This line is required.

Example 2 — Change the excerpt length

Some filters pass a simple number. Return a new number to change the setting.

WordPress
function ctf_excerpt_length( $length ) {
  return 20;
}
add_filter( 'excerpt_length', 'ctf_excerpt_length' );

How It Works

  1. excerpt_length passes the default length (55 words) as $length.
  2. The function ignores it and returns 20.
  3. WordPress now cuts every automatic excerpt after 20 words.

Example 3 — Use a second value (the post ID)

The the_title filter also sends the post ID. Ask for it with $accepted_args.

WordPress
function ctf_mark_sticky( $title, $post_id ) {
  if ( is_sticky( $post_id ) ) {
    $title = 'Featured: ' . $title;
  }
  return $title;
}
add_filter( 'the_title', 'ctf_mark_sticky', 10, 2 );

How It Works

  1. The 2 at the end tells WordPress to pass two values: the title and the post ID.
  2. Sticky posts get Featured: in front of the title.
  3. Other posts are returned without any change.

Frequently Asked Questions

It attaches your function to a filter hook. When WordPress runs apply_filters() on that hook, your function receives the value, changes it, and returns it.
add_action() runs code at a moment and ignores any return value. add_filter() changes a value and the function must return it.
The value becomes empty (null). A post may show no content or a blank title. Always end your filter function with return.
Yes. They run one after another, ordered by priority. Each one gets the value left by the previous one.
the_content, the_title, excerpt_length, excerpt_more, and body_class are easy places to start.

Next: apply_filters()

Pass a value through every filter on a hook and get the result back.

apply_filters() 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.

8 people found this page helpful