What does apply_filters(...) actually do in WordPress? What does apply_filters(...) actually do in WordPress? wordpress wordpress

What does apply_filters(...) actually do in WordPress?


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


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.


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.