What are WordPress Hooks? (The Definitive Guide)


Published at tips tricks by Jacek Piotrowski on 29th Oct 2021

One of the most significant advantages of WordPress is its rich customization possibilities. However, if you want to get the most out of the platform, you need to go beyond using just CSS and HTML. Thankfully, there’s a way to influence how WordPress “behaves” without having to edit its core. Not to mention that doing the latter is neither convenient nor safe. But that’s where WordPress hooks come into play.

In this guide, we’ll dive into what are WordPress hooks, why you need them, as well as discuss the different types of hooks. But, before we jump into showing you specific hooks and how they work, we should start with the basics. So, let's answer the fundamental question: what are WordPress hooks in the first place?

Table of Contents:
1. WordPress Hooks – Their Definition and Purpose
2. The Different Types of WordPress Hooks
    WordPress Action Hooks
    WordPress Filter Hooks
    Custom WordPress Hooks
3. Filters vs Actions
4. Working with WordPress Hooks – The Basics
    The Name of the WordPress Hook
    The Function
    The Priority
    Function Arguments
5. Adding WordPress Hooks to Your Website
6. How to Remove WordPress Hooks
7. WordPress Hooks: Time to Start Experimenting

1. WordPress Hooks – Their Definition and Purpose

An illustration showing WordPress logo next to literal hooks

When talking about WordPress hooks, there are in fact three different terms that you need to understand and pay attention to. A hook, an action, and a filter.

First, think about a standard WordPress website. Whenever such a site gets loaded in a web browser, dozens of lines of code need to be executed. The WP core, the plugins, and the theme all work together to present you with the result that you – the visitor – see on your computer (or mobile device) screen.

A WordPress hook allows you to “hook” somewhere into that process and add or modify the code that gets loaded. Themes, plugins, even WordPress itself – they all use hooks to influence that code execution process.

Of course, sometimes you may want WordPress to trigger a specific action. Other times, you may need it to return a value. Both goals can be achieved using hooks. And that’s exactly where said actions and filters – the two distinct types of hooks – come into play.

2. The Different Types of WordPress Hooks

As of today, WordPress features over 2000 different hooks. Of course, as time goes by, some of them get depreciated, while new hooks get introduced. Luckily, you don’t need to know all of them to understand what are WordPress hooks. In fact, you probably won’t need to use more than a few hooks at a time! Whichever hook you use, keep in mind that they all fall into one of two WordPress hooks categories:

WordPress Action Hooks

Actions allow you to influence what WordPress is “doing”. Typically, whenever you see add_action() somewhere in the code, it’s an action hook. Here you can see an action hook in a function used to get the date of when the post was last updated:

An example of WordPress action hooks found somewhere in the functions.php file

As the name suggests, action hooks focus on triggering an action. Simply speaking, you use actions to hook a function and execute it in addition to a different function. Unlike filters, they do not return any value

For example, to hook into creating or trashing of a post, you’d need to use 'create_post' and 'trash_post' hooks. Similarly, for categories, you need 'create_category' and “trash_category” action hooks respectively.

But hooks related to trashing or creating posts or categories are not the only ones you can use. There are hundreds of WordPress action hooks that you can use to trigger WordPress functions. But, coming back to our posts example, let's look at the 'publish_post' hook. For example, the following would send a notification any time you publish a new post:

add_action( 'publish_post', 'send_notification' );

Of course, that’s one of the simplest action hooks you can use to send that email. There’s a lot you could do here to modify the function. You could add post title to the message, or change function execution priority. In fact, you could even customize the message itself!

WordPress Filter Hooks

You can use filter hooks to change the data during the execution of WordPress and always return a value. And, to put it in less confusing terms – they allow you to modify what you see on your screen. Usually, you can recognize them by the add_filter() part:

An example of WordPress filter hook found somewhere in the functions.php file

For example, they can be used to style page or post elements; capitalize the title or change image sizes. Here’s a hook you can use to change a WooCommerce product image:

add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image' );

Just as is the case with WordPress action hooks, there are hundreds of filter hooks you can use to modify your website.

Custom WordPress Hooks

Most WordPress developers focus on using the existing action and filter hooks. But not all developers know it’s possible to define your own custom hooks.

Because it's a more advanced topic, it’s not something that’ll dive deep into doing in this guide. Still, it's worth knowing that you can use custom hooks to allow other developers to hook their functions into your code. That way, they can easily add modifications and new features on top of what you and your team have created.

3. Filters vs Actions

The easiest way to understand what are the different WordPress hooks? Think about the idea of a WordPress hook like this. You can use action hooks to “do” something. Actions hook your code to interrupt the code flow and return back to the flow once that code gets executed. But, unlike filters, they do not modify anything. For them to work, you need to wrap them in add_action( ).

At the same time, filter hooks allow you to “customize” something. That modification is then passed back to the code flow so that other functions can use it.

For these to work, you need to wrap them in add_filter(). Of course, quite often, actions and filters work hand in hand to provide you with the desired result. Here’s a piece of code used to enqueue functions and styles from a child theme.

An image explaining what are WordPress hooks by showing both types of hooks together in a piece of code.

Additionally, this comprehensive list of hooks offers an easy look at which hooks fall into each category.

4. Working with WordPress Hooks – The Basics

Once you understand what are WordPress hooks, it’s time to look into their “anatomy”. Each WordPress hook consists of the name of the hook, function, priority, and function arguments. Often, the latter two are added to the hook by default. Still, nothing is stopping you from specifying their values manually. Here’s an example of an action hook:

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

And here’s a filter:

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

So, let’s take a closer look at the first element.

The Name of the WordPress Hook

This is the first part of what comes wrapped into add_action() or add_filters(). In the below example, that name is ‘hook_name'. In real life, you could, for example, use the post_status hook.

add_action ('hook_name', 'custom_function', 10, 1);

Similarly, for the below action hook, the name of the hook will be ‘activated_plugin’

add_action( 'activated_plugin', string $plugin, bool $network_wide )

The Function

Next, add the function that you want to trigger. It can be any PHP function (including a core WP one) or one of your own.

Just keep in mind that PHP requires you to choose a unique name for your function. Here’s a list of several hundred WordPress core functions. In the below example, we want to trigger ‘custom_function’:

add_action ('callname', 'custom_function', 10, 1);

And, coming back to our WooCommerce example, you can see the ‘custom_new_product_image’ function hooked in a filter:

add_filter( 'woocommerce_cart_item_thumbnail', 'custom_new_product_image' );

The Priority

As mentioned earlier, adding priority is not essential – the default priority is 10. But, in some cases (when adding many different functions and hooks), doing so can be crucial.

To help you manage how the functions get executed, you can assign a priority value between 1 and 999. Keep in mind, though, that the value has to be an integer. In our example, we manually set priority to 10.

add_action ('callname', 'custom_function', 10, 1);

Note that if you assign the same priority (or leave it as default) to all WordPress hooks, they’ll run in the default order. This may cause some issues. This is true especially for filters, which might overwrite one another.

The Function Arguments

Lastly, you may want to specify how many arguments the function that you want to trigger can accept. If you don't know what to do with it, here's the good news. Most of the time, you don’t have to specify anything here.

But, keep in mind that some filter hooks can pass more than one argument. Because of that, you may want to make the number of variables more specific. In our example, we’ve set the value to 1.

5. Adding WordPress Hooks to Your Website

The easiest way to add a WordPress hook to your website is to edit the functions.php file. You can find it by going to WordPress > Theme Editor:

A screenshot of the dashboard appearance menu

Of course, no matter how well you know what are WordPress hooks, never do any significant changes to your website in production. Ideally, you should experiment only in a separate test/staging environment. Also, it’s not the best idea to edit the default functions.php file. Instead, consider adding a child theme to your website. To do that, you can use a free plugin like Child Theme Configurator by Lilaea.

Once configured, it creates separate functions.php, style.css (or any file that you choose). This helps reduce the risk that you overwrite important stuff and crash your website. Additionally, a child theme prevents any theme updates from overwriting your WordPress hooks. This makes your site edits upgrade-proof.

Screenshot of Child Theme Configurator settings

Naturally, don’t forget to create a backup of your website before you add any significant changes. And, thanks to WP Blazer’s robust backup options, you can choose different schedules, storage options, and even backup types.

6. How to Remove WordPress Hooks

Of course, you might also want to remove (unhook) certain actions or filters. That’s especially once you understand what are WordPress hooks (and can decide that you don’t need some).

For example, you may suspect that a hook is causing a conflict, which, in turn, causes the website to crash. Or, you may want to “turn off” certain core WordPress functions. Alternatively, you may also want to move the hook elsewhere in the code. Typically, there are two ways you can go about it:

  • You can remove that piece of code entirely
  • You can unhook and then re-hook selected actions or filters.

Let’s look at the first example. At first glance, it sounds like the most bullet-proof way to do that - after all, you remove that piece of code entirely. But, there are three potential problems that you may face:

  • You may accidentally remove too much code, causing certain functions to break. As a result, instead of fixing your website or turning off certain functions, you drive the entire site to crash.
  • By removing the code, you can’t easily add the function back to the website. Of course, if you’re 100% sure you’ll never use it again, it may make sense to do that. That’s as long as it’s not inside WP core files (see below) or nested deep inside some other functions. Still, we recommend unhooking actions and filters whenever possible.
  • Removing certain filters and actions might require you to edit WordPress core files. While it’s possible to do that, it’s not exactly WordPress best practices.  

This leaves us with the second option – unhook and then re-hook selected actions or filters. You can do that by using remove_action() and remove_filter().

So, let’s assume you want to prevent WordPress from sending email notifications whenever a post gets published. In this case, you’d need to add the following piece of code to your website.

remove_action( 'publish_post', 'send_notification' );

Keep in mind that if the particular action or filter has a priority other than the default, you’ll need to specify it when you unhook it.

So, for a function that has priority set to 1:

add_action( 'publish_post', 'send_notification', 1 );

You need to add the same priority when unhooking it:

remove_action( 'publish_post', 'send_notification' 1 );

7. WordPress Hooks: Time to Start Experimenting

WordPress hooks are a powerful tool in the arsenal of every developer. Of course, initially, they might seem difficult and overwhelming. But, mastering the hooks allows you to “manipulate” WP core without actually touching any of its files.

Thanks to hooks, you can often implement big changes to your website in a matter of minutes. Naturally, that's assuming you know which functions you need to use! And, even if you make a mistake – because the code is much clearer, debugging it becomes much easier too. Not to mention that understanding how they work is key to developing your plugins and themes.

So, what’s the best way to understand what are WordPress hooks? Take action and practice!  

Of course, while WordPress hooks provide you with a pretty secure way of tweaking your website, you still need to be careful. After all, you don't want to lose all the hard work, do you?

Sometimes a single mistake somewhere in the code can crash the whole site. And the sooner you can get the site back online, the faster you'll find a working solution.

That’s why, before you make any edits, we remind you to back up your WordPress website. And if you want a convenient way to do that, check out all the great backup options that come with WP Blazer. Start a 14-day free trial and keep your website secure.