Learning about WordPress Hooks (Actions and Filters)

Wordpress Hooks

WordPress, the most popular CMS in the world, had started its journey as a simple blogging platform way back in 2003. Till now, with default installtion, what you get is all that a blogger need. Then What has changed along the way? The answer lies in the limitless flexibility and extensibility that has emerged with the passage of time. Everybody who has used wordpress know about the tremendous availability of plugins and themes of varying nature. All these plugins and theme-options that provide additional functionality is made possible because of its great, simple to use API (application processing interface). And the credit to this flexibility and extensibility mainly lies in the Hook system that we are going to explore in this tutorial.

Every CMS, be it WordPress, Drupal, Magento, ModX, Moodle to name a few, alarm its users that

Don’t Hack Core

and same is the case with WordPress. Instead the core developers advise for using the Powerful Hook system for anything additional you need.

WordPress Core Developers say that we don’t want to overburden the core in order to keep the platform small and light, thus not to give everything to everybody. Hence we are providing you this hook system so that you can extend it to your specific needs.

Understanding Hooks:

The bulk of WordPress API is made up of two core elements categorized under one name i.e HOOKS. These elements are ACTIONS and FILTERS. Conceptually these are different but in practice these are almost similar.

  • Actions: Actions are event driven i.e. when a specific event occure such as saving a postpublishing a postloading an admin pagesending HTML to browser etc.
  • Filters: Filters, on the other hand, modify content, that could be changing the default excerpt more into custom defined Read More LinkAppending or pre-pending anything to the text in the post or anything that you want to change the text anywhere. Filters basically take data and return in after processing and actions are event driven, so they don’t require any data.

Calling a Hook:

Hooks are called by using add_action(); and add_filter(); Hook. Conversally these can be removed using remove_action(); and remove_filter(); You might use these removing Hooks whenever you find conflicts with your installed plugins or you want to remove any default core functionality implemented through hook system that is not aligned with the specific need of a project.

Anatomy of the Hook:

Hooks are basically function calls. These calls mainly require two arguments. First is the Hook name and second is Callback Function (in which you define your custom functionality that you want to incorporate). These Callback function is generally defined in themes function.php file or in plugin, but it can also be wordpress core FunctionClass or method name.

If the callback is the custom defined funcion then second argument would be a string i.e. function name as below:

 function my_custom_function(){

// do something


add_action('hook_name', 'my_custom_function');

and if you are using OOP and need to call a method from a class then make a call like this:

add_action('hook_name', array('className', 'methodName'));

Normally these two arguments are used. Although there are two more optional arguments that can be used for hooking. The third argument is for priority i.e. when there are large no. of callbacks assigned to a particular hook. Priority is similar to rank. i.e. small no. high priority. Vary from 1-10. 10 being default. Fourth argument is the no. of arguments the callback function accept. Default to 1.