Tutorial

WordPress Filter Hook Explained

As definition says Filter hooks are used to manipulate the text/content before being displayed on browser screen. So we will leans Wordpress Filter Hooks in this article.

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 [code]the_content[/code].

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.

[cc lang=”php”]
add_filter( ‘the_content’, ‘expert_content_filter’);
function expert_content_filter($content)
{
return $content. “This is my custom content.”;
}
[/cc]

Making Author Name UpperCase

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

[cc lang=”php”]
add_filter( ‘the_author’, ‘expert_to_upper’);
function expert_to_upper($content)
{
return strtoupper($content);
}

[/cc]

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.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *