WordPress Filter Hook Explained

  • Avinash
  • 1
  • Jul 30, 2012
  • Tutorial

In previous article we have covered the Action Hooks in WordPress. In this article we are about to explore the Filter Hooks in WordPress.

But before starting first have a look at difference between action hook and filter hooks.

Action Hooks can be executed on certain events while filter hooks executed for text and/or content manipulation. So let’s start with this article then.

What is WordPress Filter Hooks?

Same as action hooks we are about to take official information rather than making our own definition.

Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. Your plugin can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API.

As definition says Filter hooks are used to manipulate the text/content before being displayed on browser screen.

Let me know give you few examples for Filter Hooks in wordpress.

  1. Adding some text to post/page content
  2. Altering the Post/Page title
  3. Altering the Comment Author name

Usage of the Filter hook is same as action hook. Create function and hook that function to appropriate filter functions. That’s it.

I will give you some examples where we can see Filter Hook in working. :)

Very first we will use most used (as per me) Filter hook which is the_content.

Filter hooks are used to manipulate the text/content before being displayed on browser screen.

Appending Text to Post Content

In this example we will add some fixed content to all posts content, Let’s have a look at below code block for the same.

add_filter( 'the_content', 'expert_content_filter'); function expert_content_filter($content) { return $content. "This is my custom content."; }
add_filter( 'the_content', 'expert_content_filter');
function expert_content_filter($content)
{
    return $content. "This is my custom content.";
}

Making Author Name UpperCase

In this example we will make all author name to UPPERCASE.

add_filter( 'the_author', 'expert_to_upper'); function expert_to_upper($content) { return strtoupper($content); }
add_filter( 'the_author', 'expert_to_upper');
function expert_to_upper($content)
{
    return strtoupper($content);
}

Isn’t it looks much easy? :) Yes it is, all you need to find is appropriate Hook for your need and you are done.

Conclusion

At the end we can say that Filter Hooks are used to manipulate the text/content of the post/page. What next? In next upcoming article we will create our own Hook.

If you don’t want to miss any upcoming article in this series then subscribe to our RSS Feed via mail, Like us on Facebook or Follow us no Twitter. If you do so, I am sure you will never miss any article/freebies or updates from us.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: