Hooks, Actions & Filters

In WordPress a hook is the means by which you can modify or augment WordPress functionality without editing WordPress core files. Hooks provide a flexible way to hook your own code into the core code of WordPress, themes, and plugins and change their functionality.

A hook can be considered a named event that can be used as a trigger to execute some additional code. WordPress generates these events as it runs and developers write code to react to an event. There are two types of hook; action and filter hooks. Actions do something new and stop whereas filters change something that was already happening.

In the case of an action, WordPress is processing and gets to a point in the code where a developer might want to add in a new function, so it has a hook that says to the developer “you can do something new here if you want”. With a filter, WordPress is processing and it gets to a point where a developer might want to change the value of the data that is being processed and it has a hook that says “you can change the data here if you want and then I will continue doing what I was doing with the changed data”.

For example, you might create an action that sends an email to your friends whenever you post on your blog. You might use a filter to add some header text to an email that is already being sent. In these examples, without the action, the email does not get sent; without the filter, the email still gets sent but without the added header text.

Just to make things a little more confusing, you can also use filters as action hooks. As long as you return the passed value, your filter can do whatever else it likes.

Actions and filter are php functions. When you write an action or filter you need to register it to the appropriate hook.

add_action( hook, action, priority, number of arguments );
add_filter( hook, filter, priority, number of arguments );

Wherever there is an action hook in the WordPress code, WordPress will run

do_action( action, arguments );

for every action that has been added to this action hook. Similarly, wherever there is a filter hook in the WordPress code, WordPress will run

apply_filters( filter, data value, arguments );

for every filter that has been added to this filter hook. Filters should return the, possibly modified, data value for onward processing by WordPress.

Where there are multiple actions or filters for a particular hook, the order in which they are executed may be important. For example, you might have two different filters on the same hook that change the way some text is displayed. One puts all the text in capitals and the other adds in some additional text. You might want to ensure that the added text is also put into capitals and so the filter to add text must be run before the filter that puts it all into capitals.

You can ensure the order in which filters or actions are run by setting the priority appropriately – lower priority numbers are run first, and where the priority numbers are the same, the actions or filters are run in the order in which they were added to the hook.


You can also add an action to a hook where the action is named using a variable e.g.

do_action( "save_post_{post->post_type}", $post_ID, $post, $update );

in this example, the dynamic portion of the action name, $post->post_type, is the post type of the particular post being saved.