Published on

Hooks: Actions and Filters in WordPress

Hooks in WordPress plays a vital role in the development cycle of any application in WordPress. Hooks allows the users to simply tie your custom code to the rest of the WordPress core base. In this article we will be looking onto what exactly are the Hooks in WordPress and some examples on using them.

A hook is generally referred as a process of attaching a component or a function that modifies the default functionality of WordPress or vice-versa. So, Basically, there are two types of hooks in WordPress.

  1. Actions
  2. Filters

Action hook are generally triggered by specific events that take place in WordPress, such as publishing an article, updating, or even initializing your custom code that is hooked into a certain action.

Similarly, Filters are filters. It filters the desired content passed accordingly. It acts as a middleware between the database and the browser and vice-versa. Some example would be to filter out a content by customizing the content on how it is displayed.

In simple terms Action is used in execution level. That means action is used when you want to run a certain piece of code in the WP runtime. This not modifying or receiving piece of data. Similarly, Filters allow you to fetch a piece of data and then return it back using custom functions.

Adding Actions and Filters

For action hooks it is pretty straight forward. The process is quite simple. In order to execute an action you will need a piece of information which the action can process. The piece of information will be our function. We hook that function by following.

Hooking Actions
add_action('hook_name', 'your function name', [priority], [accepted $args]);

Hook_name and Function name are the required parameters here. Others are optional. The priority is based on a integer scale of 1 to 9999 that determines the priority order of functions those are tied to that specified hook. Default is 10. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order added to the action. Accepted Arguments are an optional integer argument defining how many arguments your function can accept (default 1), useful because some hooks can pass more than one argument to your function.

Hooking Filter
add_filter ( 'hook_name', 'your_filter', [priority], [accepted_args] );

The add_filter works same as add_action. A filter function takes as input the unmodified data, and returns modified data (or in some cases, a null value to indicate the data should be deleted or disregarded). If the data is not modified by your filter, then the original data must be returned so that subsequent plugins can continue to modify the value if necessary.

Removing Actions and Filters

To remove a action or a filter is very simple. Use the remove_action or remove_filter for removing them with the name of the hook, function and priority. Note that if a hook was registered using a priority other than the default of 10, then you must also specify the priority in the call to remove_action(). Also note that in general, you shouldn’t remove anything unless you know what it does and why it does it — check the WordPress or other plugin source code to be sure.

remove_action( 'tag', 'function to remove', $priority );
remove_filter( 'tag', 'function_to_remove', $priority );

Examples of Action Hook

WordPress has following actions available in the Action Reference Page.

Removing Certain Admin Menus
function remove_certain_menus() {
    remove_menu_page('edit.php'); // Posts
    remove_menu_page('upload.php'); // Media
    remove_menu_page('edit-comments.php'); // Comments   
}
add_action( 'admin_init', 'remove_certain_menus'  );

In the example above you can see that remove_certain menus is being hooked to admin_init action hook. So, this code runs when the admin is initialized. The action will remove certain menus from admin.

Hooking Script into Footer
function footer_scritps() { ?>
<script type="text/javascript">
   jQuery(document).ready(function($){
     //Your jQuery Code
   });
</script>
<?php 
}
add_action('admin_footer', 'footer_scritps');

The above code will add the scripts into the html footer in admin section.

Examples of Filter Hook

WordPress has following filters available in the Filter Reference Page.

Changing the Excerpt length
function change_length_except( $words ) {
 return 30;
}
add_filter( 'excerpt_length', 'change_length_except' );

In the above code we used excerpt_length filter which gives us the determined integer length from the_excerpt and then filter that with our return value from change_length_excerpt function.

See more on Hooks

This article is basic guidelines for using hooks in WordPress. You can get complete reference from WordPress Codex. Action References are here. Filter references are here.