A Practical Guide to Understand WordPress Hooks and Their Role in Custom Development

WordPress is lauded for a lot of things, not the least are the number of features and functionalities it has to offer us, but the comfort it provides to take our website to the next level. The ease with which you can modify the themes and leverage the benefits provided by its plethora of plugins, while maintaining a wide user base is what makes this CMS one of its kind.

If you have been a WordPress user for a considerably long period of time, then you must be familiar WordPress hooks. WordPress Hooks are unquestionably an integral part of WordPress development and get their share of discussion quite frequently. It is a function that is related to the WordPress customization, extension, and the development process through an API that resides in our themes, plugins, other enhancement related ventures.

Modifying WordPress core files can be a dangerous step, so whenever you need to alter them or need to construct a new functionality, it is advisable to turn to hooks. In this post, I will discuss about the basis of WordPress hooks and how their role is significant in determining the way you ‘code’ in WordPress and how they enhance the behavior of your site.

Types of WordPress Hooks

There are basically two types of WordPress hooks- Actions and Filters.

‘Actions’ allow for the insertion of custom code, while the latter one allows you to manipulate the various bits of content. Using actions one can add extra functionality  at a specific point- for example you might want to add extra widgets, menus, or any kind of promotional item within your website. ‘Filters’, on the other hand, allow you to modify the data being processed- for example you might want to add a CSS class or want to make some changes within certain pages and so on.

Going Into the Details

Action Hooks

Talking about the action hooks, these are some specific points in WordPress core, themes, and plugins which allows some external resources to add an additional code, and then customize it, so that you can achieve an entirely new functionality. A commonly used action hook is ‘wp_head’. This action is mainly used by various themes and plugins to insert things such as meta data that sits between the <head> and </head> and </head> tag of a WordPress page.

So, to hook into an action, first of all , we need to create a function in our theme’s functions.php file and hook it with the help of add_action ()functions. To hook your code into an action hook you can use the statement like this:

add_action( ‘action_hook’, ‘your_function_name’ );

The above statement will help WordPress to know that you would like to execute a function called ‘your_function_name’ whenever the system will encounter a action hook called action_hook.

This is how an action hook can be used:

<? php

add_action( ‘wp_head’, ‘my_actionhook_example’ );

function activated_plugin_actionhook_example () {

echo ‘<meta name=”description” content=”Hello World! This is the meta description created for this page.” />’ . ” “;

} // End my_actionhook_example()


This is the code which you can to be placed inside your theme or within the functions.php file of your child theme. When executed it will produce a meta tag instead of head area of your WordPress page.

Filter Hooks

Filter Hooks are used to modify the content before adding it to the database or rendering it to the browser screen. There are two ways to identify filter hooks.

apply_filters( $tag, $value );

apply_filters_ref_array( $tag, $args );

You’ll need to find the following line of code within the WordPress core:

$title = apply_filters(‘wp_title’, $title, $sep, $seplocation)

The above example showcases the creation of a filter hook called ‘wp_title’. This hook will be helpful in altering the title of the page.

Here, $tag is the name of the hook and there are also some registered parameters which are passed to the hooks for further functionalities.

The apply_filters is used to pass a parameter to the filter. It is actually a customized function we write. In return, the filter will always return the $value parameter to the WordPress.

Creating a filter follows the same approach to generating an action. Let’s assume you want to create a function which is capable of performing something for this you’ll need to register your filter to a particular hook by using the following code.

add_filter ( ‘hook_name’, ‘your_filter’, [priority], [accepted_args] );

Here, Hook_name is the name of the hook you wish to register, ‘your-filter’ is your filter name, ‘priority’ is an optional function which can be used to demonstrate the order you want to apply to your filters. If it is not provided with any value, it would represent its default value which is 10.

accepted_args is another optional argument which aids in defining the number of arguments your function is able to accept. It is mandatory for a filter to at least accept one parameter, which can be returned.

Now, look at the below example to know how a filter can be registered.

<? php

add_filter( ‘wp_title’, ‘mytest_add_sitename_to_title’, 10, 2 );

mytest_add_sitename_to_title( $title, $sep ) {

/* Retrieve site name. */

$name = get_bloginfo( ‘name’ );

/* Append site name to $title. */

$title .= $sep . ‘ ‘ . $name;

/* Return the title. */

return $title;



In the above example you can see how we are registering a filter with the name mytest_add_sitename_to_title to the wp_title hook. Notice that we have also assigned a priority to 10 and have determed 2 arguments which will be accepted by our filter function.

It is worth noticing that our filter will ‘return’ the title back to the WordPress so that it can be displayed on the page.


I hope you’ve gained a pretty good understanding of WordPress hooks and how they define the whole process of WordPress custom development.