143

I'm trying to understand some of the function in WordPress, but I can't get my head around what apply_filters(...) actually does.

Is someone able to clear this up for me with a few examples?

5
  • 12
    Awesome: dev.themeblvd.com/tutorial/filters Apr 23, 2014 at 21:28
  • A note that apply_filters() is an incredibly misleading name for the function as it applies both filters and actions hooked onto the target function Dec 4, 2015 at 13:14
  • @NabilKadimi, great suggestion, dev.themeblvd.com/tutorial/filters/#highlighter_604585, but when this function does not accept any arguments(please click the link to see which function I am talking about), how is the $output variable altered , as he describes in his next code section? Jul 16, 2017 at 13:41
  • @RameshPareek You're link doesn't seem to work as expected, please use a Github gist. Jul 16, 2017 at 20:51
  • sorry! Please scroll down to Example 1: The Basics on that page. Jul 17, 2017 at 12:06

4 Answers 4

121

apply_filters($tag, $value) passes the 'value' argument to each of the functions 'hooked' (using add_filter) into the specified filter 'tag'. Each function performs some processing on the value and returns a modified value to be passed to the next function in the sequence.

For example, by default (in WordPress 2.9) the the_content filter passes the value through the following sequence of functions:

  • wptexturize
  • convert_smilies
  • convert_chars
  • wpautop
  • shortcode_unautop
  • prepend_attachment
  • do_shortcode
1
  • Also remember to echo anything that you are not using the value of if it seems that apply_filters($value) is wiping out a value...
    – MrMesees
    May 15, 2016 at 17:01
33

late answer

Short explanation

apply_filters() interacts with the global $wp_filters array. Basically it just checks the array if the current filter (or hook) has an action(/callback function) attached and then calls it.

Long explanation

When you attach a callback/action to a filter or hook, then you just add the callback name to global filters array. When then, in code (for e.g. a template, core or plugin file) a call to do_action() or apply_filters() happens, then WordPress searched through the array and calls the callback. The only thing more special with filters than with hooks is, that it returns the value (for further handling) instead of just firing the callback. So summed up: Hooks are to insert data, while filters are to modify data.

20

Here's what I'm gleaning, upon considering the most popular answer and additional resources:

  • $tag seems to be a synonym for the name of the hook. (That's not particularly intuitive to me.)
  • the_content is an example of a hook, of the "filter" type.
  • the_content hook consists of multiple filters.
  • Filters modify data. They basically filter the database, changing the data before the users view it.
  • A common use of apply_filters(), for instance, is to apply the_content filters to $content. In this instance, double returns will convert to <p> tags, smiley faces will convert to icons, etc.
  • "the_content" is a hook, while "the_content()" is a function.
1

In the most basic terms, apply_filters is used to initialise a filter hook... add_filter assigns a new function to hooks that have already been created.

2
  • 4
    Sorry, this is the wrong way around: apply_filters() calls the filter hooks that are already initialized and add_filter() is the call that creates a filter hook. I know you actually meant this, but you have the functions around the wrong way!
    – Brian C
    Mar 8, 2013 at 23:11
  • apply_filters($tag,$value); allows you to create a place in your code where you can then allow the $value you pass to be hooked into and modified by using add_filter();
    – Styledev
    Oct 31, 2013 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.