Action and Filter Functions In WordPress

Thanks to the developers of WordPress, making a website (especially a blog) is now easy and simplified. With tons of plugins, themes and widgets a user can now make his web pages very interactive and user-friendly. However, there are tons of goodies for the developer as well. (Happy developers make happy users!) Here I am sharing my knowledge about most effective Action and Filter Functions In WordPress.

Understanding Action and Filter Functions In WordPress

Most plugins and themes on WordPress are ‘extendable’. That is because functions in WordPress are easy to use and customizable. Extendable means that we can add additional functionality to the existing plugins and themes and customize it to enhance the user experience. In this post, I will explain how you can do so.

Action and Filter Functions In WordPress

Before we begin, we need to know about something called “hooks.” By definition of WordPress Codex, Hooks are provided by WordPress to allow your plugin to ‘hook into’ the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion. In other words, Hooks are the places in your code where you can customize the functionality of your plugin by calling functions using their labels.

There are two kinds of hooks:

  • Actions: Actions are hooks that help you call a function when a particular event occurs. For example, a post is published, or a theme is changed.
  • Filters: Filters are another kind of hook. Filters help you edit the already existing data.

Filters and Actions are very closely related. However, we can still say that Filters are the superset of Actions. Superset of actions means Filters can do pretty much everything that Actions can do. They are, in my opinion, based on the choice of semantics.

Difference between Action and Filter functions in WordPress

Triggered by specific events that take place in WP.They are functions to which WP passes data at certain points in execution, just before taking some action with that data.
Does not have to return a value.Always return a value.
Allows the developer to “ADD” some code to the existing one.Usually “MODIFIES” the current data.

Some great hooks are the below. They are commonly used by developers.

  • do_action($tag)
  • add_action($tag, $function_to_be_called)
  • apply_filters($tag, $value)
  • add_filters($tag, $function_to_be_called)

Let’s take an example to explain the usage of these functions. Say we are running a restaurant called “Burger Queen.” For Easter, we have an offer running on all our chicken burgers. The offer is “Cheese it!”. A customer ordering on the website will go through the following changes. In this case, this is what we have to add in our PHP file:


Later in the function, we can call the function associated with the label ‘show_offer’ using add_action.

[php]add_action(‘show_offer’, ‘offer_function)
function offer_function()
echo “Cheese it! Free cheese with a chicken burger”;

The filter function will be used by the choice of burger

[php]if ($filling == ‘chicken’)
apply_filter(‘add_cheese’, ‘chicken’);

Similar to the add_action, the apply_filters will be called later in the code and will look something like this.

[php]add_filters(‘add_cheese’, ‘cheese_function’)
function cheese_function($filling_type)
return $filling_type . ‘and cheese’;
(Here $filling_type will be ‘chicken’)[/php]

There are other hooks in WordPress. You can check out like has_filter(), apply_filters_ref_array(), current_filter(), remove_filter(), remove_all_filters(), doing_filter(), do_action_ref_array(), did_action(), remove_action(), remove_all_actions(), doing_action().

For more information on the arguments passed to the above hooks, or Action and Filter Functions In WordPress, please check out Let us know what you think in the comments below. If you have any suggestions, then include them as well. We would love to add them and update the post.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.