Search

How to Add Filter and Action Hooks when developing Plugins

    Aparna Gawade
Listen to this article

When developing WordPress plugins or themes, you find yourself quite often using hooks, to modify or extend some functions. Hooks are provided for this exact very reason. An option for developers to add, remove or update basic operations or values. A hook literally allows you to hook in your code into the execution of an operation. There are two kinds of hooks provided; Action Hooks and Filter Hooks.

Actions Filters Hooks

 

Actions Add Functionality

An ‘Action’, gives an indication of an event that has happened. An Action Hook provides an opportunity to hook in your function, after the event has occurred. For example, ‘init’ is a default action hook. The ‘init’ event is fired after WordPress has finished loading, but before any headers are sent. When this event is fired, all actions hooked on this event will be called.

For example, say on ‘init’ hook you want to do some action; you will have to add your function as follows:

add_action( 'init', 'mycustom_action' );
function mycustom_action()
{
  // write something here
}

 

Filters Modify Value

A Filter is basically used to set an option value in the database. The value is usually set before some operation is performed, to modify some parameters of the operation. For example, consider your theme displays 10 latest posts on the homepage. If you wanted to display only 5 posts, you will have to use the ‘pre_get_posts’ filter and set ‘posts_per_page’ to 5.

add_action( 'pre_get_posts', 'mycustom_filter' );
function mycustom_filter( $query ) {
   if ( $query->is_home() && $query->is_main_query() ) {
       $query->set('posts_per_page', 5 );
   }
}

[space]

Adding Hooks when Writing Plugins

When developing plugins, it is always a good practice to include hooks, so as to allow easy extension or modification of the plugin functionality.

Adding Action Hooks

A good place to add action hooks is before and after performing some operations, like displaying some data, redirecting to a new page, before executing some main function etc. For example, if you see the WooCommerce single product page, several hooks are provided to completely reorder the content displayed on the page.

The action hook is the simplest to add. All you need to do is use do_action(‘action_name’), at a point in the program, when you want to allow functions to be added. For example, say you have a function in your plugin, which displays social sharing icons on a page. To allow users to hook actions, before or after the social sharing icons are displayed, you will have to add the two hooks as follows:

//social sharing icons display function
function mcp_disp_ss_icons()
{
  //allow users to hook in before display function is called
  do_action('before_social_icons_disp');

  // actual function code here…

  //allow users to hook in after display function
  do_action('after_social_icons_disp');
}

[space]

Adding Filter Hooks

If your plugin or theme saves some values in the database, and you want to provide the option for a user to edit these values, it is good practice to provide filters for these values. You have to first declare a function which filters data, and then register it using the ‘apply_filter’ function.

Since a filter is basically used to modify data, a filter functions takes a value to be filtered as the parameter, and returns the modified value. For example, if your plugin changes the post title before it is displayed, you can add a filter as follows:

//add a filter to change the title
function mcp_change_title($title)
{
  $title = $title.'updated';
  return $title;
}
// add a hook for the filter to allow changes
apply_filter('mcp_title', 'mcp_change_title', /*priority*/ 10);

If many filters are added to the same hook, the priority will decide which modified value will finally be applied. The lower the value, the higher the priority.

For additional reference read Writing a Plugin: WordPress Plugin Hooks

 

Aparna Gawade

Aparna Gawade

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads