WordPress Hooks: Everything You Need to Know About

WordPress Hooks: Everything You Need to Know About

If you’re new to WordPress development, then Hooks are something you must be familiar with. Being an integral part of WordPress, they give you the ability to customize, extend and enhance the functionality of the CMS, absolutely without modifying the core files. In this quick guide, I’ll introduce you to WordPress hooks, their types and show you how they actually work with appropriate examples. So let’s get started!

What Are WordPress Hooks?

Simply put, Hooks are some specific places in WordPress code, where you can execute your own custom code or change what WordPress does or outputs by default. Essentially, they define how one piece of code should change or interact with another piece of code in WordPress. Think of them like hooks in our real-life, which we use to hang something on.

Whenever you decide to develop a WordPress theme or plug-in, then it is almost certain that you will make use of hooks to achieve your desired functionality. WordPress offers a number of hooks that you can use to define how your theme of plug-in should interact with WordPress Core. However, if you want to let other developers modify and extend your theme or plug-in, you can also create your own hook.

Why Do Hooks Exist?

The main reason why Hooks exist is their ability to provide extendibility. Taking advantage of them, you can extend WordPress as much as you want, absolutely without modifying core files. Regardless of what you want to do – modify the existing functionality or create a new one – they provide you the best way to perform manipulation and re-usability. In a nutshell, WordPress Hooks allow you to make a theme or plug-in adapt to your own custom requirements without touching the WordPress core.

Types of Hooks in WordPress

There are two types of hooks in WordPress: Action and Filter. Where Action hooks allow you to change the way how WordPress operates by default, on the other hand, Filter hooks give you the ability to modify any WordPress data before sending it to the database or the browser. To use an Action or Filter hook, you’re required to write and then register a custom function, called callback, with WordPress.

In the simplest terms, Action hooks are used to do something, while Filter hooks are used to change something in WordPress. Let’s briefly look at each one with examples:

Action Hooks

The concept behind how Action hooks work is pretty simple. They allow you to insert your own custom code into the WordPress core at a specific point, called event. An event could be anything like activating a plug-in, changing a theme or publishing a post. As soon as a specific event occurs, an Action hook is triggered to provide you the extra functionality.

To Hook into an Action, all you need to do is to create a function in the function.php file and then hook it using the add_action() function as follows:

add_action( $hook, $function_to_add, $priority, $accepted_args );

Let’s understand the parameters used in the above line of code:

  • $hook: The name of the hook that you want to associated with.
  • $function_to_add: The name of the function your want to call.
  • $priority: An optional integer value between 1 and 999. It specifies the order in which the functions tied to a specific hook are executed. Lower numbers means earlier execution of a function.
  • $accepted_args: An optional parameter used to pass multiple arguments to a function.

So $hook and $function_to_add are two inevitable parameters of the add_action function.

One of the most essential and commonly used Action hook is wp_head. Many times you may have noticed a function, named wp_head(), which is presented just before the </head> tag in a template. If you explore the code of this function, you’ll find that it contains a call to do_action() function. That means, it will execute all the functions associated with the wp_head tag.

To understand how wp_head action hook works, let’s add wp_head action to add custom CSS within the <head> section:

function call_css() {

$output=”<style> .wp_head_example { background-color : #F25F43; } </style>”;

echo $output;

}

add_action(‘wp_head’,’call_css’);

In the above example, you can see that the function call_css being hooked into the wp_head action hook. This tells WordPress that you want to execute your function call_css when the wp_head action hook is being generated.

Similarly, to remove a hook, you need to use remove_action function as follows:

remove_action( $tag, $function_to_remove, $priority );

And if you want to find out where you can hook your own functionality into WordPress core, just search for the ‘do_action’ in an advanced code editor like Eclipse and you’re good to go!

Filter Hooks

Unlike Action hooks, Filter hooks are used to modify, manage or intercept data before sending it to the browser screen or saving it from the browser to the database. What makes them different from Actions is that they don’t perform any other function except modifying data before it goes from database to the browser and vice-versa. Some common examples of using filters are registering additional Meta field types, changing the excerpt length and adding some custom code to a template file to change the default functionality.

As another main difference, a Filter takes a value and returns it, but an Action simply runs the code and don’t return any value at the end of a function. You can hook into a Filter in the same way as you do with an Action:

add_filter( $tag, $function_to_add, $priority, $accepted_args );

As you can see, all the parameters in the above line of code are same to those were passed into the add_action function. Now let’s understand how Filter hooks work with an example:

add_filter( ‘unique_content’, ‘copyright’ );

function copyright( $content )

{

return $content . ” <br>You must not copy this content.”;

}

In the above code, unique_content is the Filter that we hooked into and copyright is the function that we want to execute. As a result, this Filter will add a copyright notice to every post before rendering the content in the browser. An important thing that you must keep in mind while using a Filter is that you must return something. Otherwise, things may break.

In fact, before serving any data, WordPress runs it through any Filters that you have created. To remove the filter, use the remove_filter function:

remove_filter( $tag, $function_to_remove, $priority );

Like Action hooks, you can identify Filter hooks using the following piece of code:

apply_filters

For example, if you search through the WordPress core, you’ll find a line of code like:

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

The above code creates the wp_title filter hook that manipulates the title of a page before displaying it in the browser.

Hopefully, you now have a better understanding of what hooks are, what their importance is and how you can use them. So use the power of Hooks and take your WordPress project to the next level!

Author Bio: Ajeet is a senior web developer at WordPressIntegration – PSD to WordPress service provider, where he is responsible for writing custom JavaScript code during the conversion process. In his spare time, he writes on different topics related to JavaScript, WordPress, and Web Design to share his work experience with others. You can follow PSD to WordPress – WordPressIntegration on Facebook.