50 Filters of WordPress: An Introduction to Filters

WordPress is an amazing platform and the most popular content management system in the world. The reason for this title is because of its extensibility. In this series, we're going to learn about filters - one of the best ways we can extend WordPress.

In the foundation of this feature lies hooks – filters and actions of WordPress. Without them, we wouldn't be able to take advantage of how extensible WordPress really is.

In this series, we're going to learn about filters, one of the best ways we can extend WordPress. It's going to be a seven-part series that focuses on:

  • the definition of WordPress filters and an introduction on how to use them (the article you're reading right now),
  • five articles about a total of 50 WordPress filters (10 in each article) with examples
  • and one "wrapping up" article, recapping the explanation of filters and the 50 examples.

There are—literally—hundreds of WordPress filters in the core and these 50 examples will be only a part of them (about 10%), so there might be an addendum if you like the series and suggest new examples about new filters.

Anyway, it's time for the introduction of WordPress filters. Let's begin!

What Is a WordPress Filter?

In the Codex, filters are defined as follows:

Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data. Filters sit between the database and the browser, and between the browser and the database; most input and output in WordPress passes through at least one filter. WordPress does some filtering by default, and your plugin can add its own filtering.

So, in essence, filters are functions doing stuff to your website's data before WordPress outputs it. A filter is one of the two kinds of hooks in WordPress – the other one is called actions which is the subject for another series of articles.

Even though it looks a complicated topic, filters (and actions) are really easy to understand. I was also intimidated when I first came across with them, but after seeing how simple they are, I got to know hundreds of filters and actions just by checking the Codex or digging the core code.

You should definitely learn about the hundreds of filters, too. But first, you need to know how to use them.

Using Filters in WordPress

Using WordPress filters is, as I said, pretty easy. There are just some basic functions to get to know and you need to learn about what each filter does. (To be honest, the hardest part is learning all the filters but as you can imagine, you can't learn every single filter at once – you learn them when you need them.) 

In this section, we're going to go through four things: 

  1. Creating a filter function, 
  2. Hooking it to a filter, 
  3. Removing a function from a filter, 
  4. Creating our own filters.

Creating a Filter Function and Hooking It to a Filter

In order to play with the data that's being passed from the filter, you need to create a function that defines how it will play with the data, and hook it to the filter.

Let's say that we're building a plugin to remove the vowels in post titles. Instead of saying "remove the vowels in my post titles", you say "hook this function (which removes the vowels) to the filter of my post titles".

Complicated? Not really. In the example below, we'll write the code of the function that removes the vowels from anything:

The function takes the $title string, removes the vowels and returns it. Easy enough, right? Now, let's take it to the next level and hook it to a filter:

Notice the new function? Let's have a look at add_filter() quickly:

  • $tag (required) - The name of the filter.
  • $function_to_add (required) - The name of the function to hook to the filter.
  • $priority (optional) - An integer to specify when our function will be executed. The default value is 10: The function will run earlier if you set it to lower and later if you set it to higher.
  • $accepted_args (optional) - An integer to set the number of arguments that the filter accepts. The default value is 1.

Removing a Filter

We can remove functions attached to filters, too. To do so, we use a simple function called remove_filter(). Let's see how it works:

The parameters are almost the same as add_filter():

  • $tag (required) - The name of the filter.
  • $function_to_remove (required) - The name of the function to remove.
  • $priority (optional) - The priority of the function (as defined when the function was first hooked).

There's also another function called remove_all_filters() which has only two parameters ($tag and $priority) where you set the name of the filter and set the priority. As its name suggests, it removes all functions hooked to the filter.

Creating Your Own Filters

Wonder how these filters are created? There's a special function called apply_filters() which is all around the core code to create hundreds of filters. Of course, it can be used outside the core which means we can create filters inside our plugins and themes, too. 

Let's see how it works:

  • $tag (required) - The name of your filter hook.
  • $value (required) - The value to be modified by filter functions hooked via add_filter().
  • $var1, $var2 and so on (optional) - Parameters of your filter (as many as you like). Filter functions can use these parameters but the they can't be returned by functions.

Let's think of an example: Imagine you write a function that only returns a famous quote by Peter Griffin:

If you want to let people filter this quote (and leave your plugin's code alone), you need to use the apply_filters() function as follows: That way, another developer uses your plugin can play with your function's data like this:

Now, every time the peter_griffin_quote() function is run, Peter's quote will be slightly changed without the developer editing your plugin file. Piece of cake!

If you need additional info on this topic, you should check out a great tutorial by Pippin Williamson on Tuts+ Code: "Writing Extensible Plugins With Actions and Filters". In this tutorial, you can learn how to create filters and actions for your plugin or your theme.

Conclusion

The more you work on them, the more fun you have with filters. There are hundreds of them and learning each one gets you one step closer to being a WordPress guru. In the next part of this series, we're going to learn about 10 WordPress filters:

  1. login_errors
  2. comment_post_redirect
  3. allowed_redirect_hosts
  4. body_class
  5. locale
  6. sanitize_user
  7. the_content
  8. the_password_form
  9. the_terms
  10. wp_mail_from

I'm really excited about this series and I hope you'll enjoy it as much as I do. If you think you can help me with the tutorials by suggesting more filters and asking for more examples, don't hesitate to tell me and share your thoughts by commenting below. 

And if you liked what you read in this article, don't forget to share it!

See you in the next tutorial!

Tags:

Comments

Related Articles