An Introduction to WordPress Action Hooks & Filter Hooks

If you are using WordPress for your blog or starting with WordPress development, this article is going help you. As you already know WordPress is the most popular CMS platform in the world empowering more than 28% sites of the web. There is something unique that WordPress done that it overshadowed other competitors in the market. And to make limitless customisations on it, is the main part of its success.

In this article I’ll discuss about WordPress development with action & filter hooks. What is this hooks & what is the difference between action hooks & filter hooks?

So let’s get started.

WordPress started as a blogging platform, but very soon it became popular & powerful as fully featured CMS. At its core WordPress provides coding stability and provides hooks to put custom PHP almost every corner of WordPress.

These hooks are great for customisations and give us developers the power to do whatever we want from it. By hooks WordPress gives us a way to put our custom PHP functions within WordPress to achieve any certain task.

Hooks are mainly divided into 2 categories.

  • Action Hooks
  • Filter Hooks

Action Hooks

Action hooks are WordPress defined way to execute our own set of codes during any particular event or action in our WordPress site. By this we can add, modify or delete any functionality during that particular action or from the whole site itself.

By using action hooks (see the WordPress codex reference for hooks), plugin or theme developers can do things like adding html meta tags to the head of a page, adding js files to footer, adding widget, register custom post types etc. Action hooks are mainly used for custom development. For plugin development this action hook is very important to know, however knowledge of it requires for theme development also.

In this function above, first parameter is the name of an action hook defined by WordPress (we can also define our own actions hooks) and the second one is the name of our custom PHP function.

Here is an example

By this code I’m adding keywords meta tags to my WordPress theme. So here wp_head  is the action to manipulate the code to head section of HTML page of the site.

Here we are sending an email to admin after publishing post. So here publish_posts  action hook is particularly hooking our custom function into the post publishing event.

In this example by using init action hook we registered a new custom post type to our WordPress install. As you can see here init is not working for any particular area or event, it is adding a new functionality to the site.

Filter Hooks

Filter hooks are another type of WP hooks which works with content and output manipulation. Filter hooks basically filter the data before presenting to the users. While developing themes, developers use it to manipulate the content while presenting it to visitors. 

Suppose our post title looks like this

This is the post title

And we want it to be like this

This Is The Post Title

This can be achieved by the filter hooks, and by using this we are not changing the actual data, we are just presenting it in our way.

Here the_title  filter hook passing the title of the post content argument to the function and within the function we use  ucwords PHP function to modify the title.

As we can modify the title of a post, we can also make changes in post content by using the filter hook.

Here we have added a social share button at the end of the content of the post by filter hook.

So by these examples and discussions I believe you get a basic overview of action hooks and filter hooks. Let’s try these codes by yourself now.

Leave a Reply

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