Menu
Disclosure: ULTIDA's content is free & reader-supported. We may earn a commission if you click & buy through our links. Your support helps us create the best content & make a difference.
Tutorials

What Are WordPress Hooks? (How To Use Them)

what are wordpress hooks

Are you trying to figure out what are WordPress Hooks?

Hooks are actions and filters that developers can build to extend the functionality of WordPress core. In general, WordPress hooks are the reason why WP is so customizable.

To put it differently, hooks allow us to extend WP’s functionality through the API in the themes, plugins, and other functions.

I think they are generally still not used enough, as users don’t understand or just ignore them.

The BEAUTY of WordPress hooks is that you can paste functions and procedures from different libraries across the internet to add new features to your WordPress theme.

You will have to be fluent in PHP coding to write WordPress hooks. It’s a default coding language used in WordPress.

This post covers:

How Do WordPress Hooks Work

The purpose of hooks is to run a certain function that enables the developer to modify, extend or even limit the options of WordPress plugins and themes.

Every WordPress website is built from several parts or modules. The website functions properly only when you put them all together.

The WordPress Core is, as the name suggests, the VITAL part of the website.

We can compare it to a finished house, but without any furniture or appliances in it.

You can still survive in a house like that, but living is not comfortable and not at all fun.

And just as you need furniture and other appliances in your house, you almost certainly need extended functionality and design improvements on your website.

Types Of WordPress hooks

There are two types of WordPress hooks, actions and filters.

  • Actions are hooks that start at a certain point when WordPress is running and allow you to take action.
  • Filters, on the other hand, let you change the processed data and return it.

In the basic principle, the difference is very simple, actions do something when called upon and filters “catch” the data and let you change it in any way you want.

How To Use WordPress Hooks

It’s important to use best practices when implementing hooks to your plugin or theme so that other users and developers can change it hassle-free.

Use the same functions that are also used in WordPress core, so add_action() / do_action() and add_filter() / apply_filters().

Any WordPress developer can create custom hooks for their themes and plugins. But that can create a PROBLEM if they use common prefixes to name the hooks.

Obviously, something like newsletter_form would be a very bad name because it’s almost certain it was already used.

When I was developing WordPress themes for PremiumCoding, we used the prefix pmc, which would be our unique prefix that was added to the hook name. So the name newsletter_form would be pmc_newsletter_form, which guaranteed that our theme wouldn’t conflict with other themes and plugins.

1. Create an action hook

Action hook is added with the activation of add_action () function in your functions.php file.

add_action(‘function name of hook’, ‘The_name_of_function’ ,’priority_scale’)

It’s important to pay attention to the priority scale, which is defined on a scale from 1 to 999.

The lower the priority number, the earlier the function will be run. The default value of the priority is 10.

There are 200+ hooks in WordPress. Let’s check an example.

function pmc_custom_menu_page() {
      add_menu_page( ‘custom menu title’, ‘custom menu’, ‘manage_options’, ‘pmc_myplugin/pmc_myplugin-admin.php’, ”, ‘dashicons-admin-site’, 6 );
}
add_action( ‘admin_menu’, ‘pmc_custom_menu_page’ );

This hook is used to add a custom menu for a plugin or a theme. It was used in all our WordPress themes developed for PremiumCoding.

What is an action hook?

Action hooks are tools within WordPress that perform functions that can be defined as actions.

They are used in themes and plugins to add or edit the functionality of the WordPress core functions.

2. Create a filter hook

Please check the WordPress Codex before you start working with filters.

It provides a detailed explanation of everything there is to know about filters. There is a list of all the filters that you can use in your WordPress themes and plugins.

What’s the deal with custom filters and custom actions?

Custom actions give you permission to add or remove code in existing actions. On the other hand, custom filters allow for data replacements within the existing actions.

You can create a filter hook with apply_filters() function. It’s used to modify certain values with new ones.

Let’s check a simple example that will change the size of your thumbnails.

apply_filters( ‘admin_post_thumbnail_size’, $size, $thumbnail_id, $post );

So, for instance, if you want to change the size of your thumbnails, you can change the filter to:

apply_filters( ‘admin_post_thumbnail_size’, 80, $thumbnail_id, $post );

Or if you want a custom size, you can set both width and height:

apply_filters( ‘admin_post_thumbnail_size’, array(80, 60), $thumbnail_id, $post);

The parameters above are just examples, you can play with the size until you find the one that fits your website best.

What is a filter hook?

The filter hook is a function that is used to modify or replace a value from your WordPress with a new one. It catches and filters those values with the associated filter hook functions (apply_filter)

Where are WordPress hooks stored?

The best thing about WordPress hooks is that they aren’t stored anywhere in your WordPress database.

Once you create an action or filter, it can be accessed globally across your WordPress website.

But the actual storing of hooks happens in a complex PHP, which I won’t touch on to keep this tutorial beginner-friendly.

(And if you actually find this PHP – DON’T touch it if you don’t know what you’re doing.)

How To Unhook Actions And Filters

It’s also important that you know how to disable actions and filters.

Why would you UNHOOK them and not just delete all the code from the plugin or a theme?

The problem is that you usually work with other developers’ themes and plugins, so deleting the correct lines of code might not be that simple.

And that code will be right back there when you update your WordPress theme or plugin.

So it’s much better to unhook them using remove_action() and remove_filter().

For instance:

remove_filter( ‘the_content’, ‘my_content_filter_callback_function’ );

3 WordPress Hook Examples

Since learning (coding in particular) is best from real-life examples, let’s take a look at some WordPress hook examples.

Many popular plugins implement the “under maintenance” functionality in WordPress, but you can also add a simple message with WordPress hooks.

1. Simple Text Message

The function below will add a simple text message for every visitor not logged into your website.

if(!function_exists(‘pmc_maintenance_message’)){
function pmc_maintenance_message(){
if(!get_current_user_id() && $GLOBALS[‘pagenow’] !== ‘wp-login.php’)
die(“This website is under maintenance!”);
}

add_action(‘init’,’pmc_maintenance_message’,10);
}

Note: Don’t forget to log out to test the changes!

2. Post Redirect

In the next example, we will redirect users to a certain post or page after they log in.

This is useful if you have a subscription-based website and you want to redirect your users after they sign up to use your services.

if(!function_exists(‘pmc_redirect_me_after_login’)){
function pmc_redirect_me_after_login(){
$postId = 1;
wp_redirect(get_permalink($postId));
exit();
}

add_action(‘wp_login’,’pmc_redirect_me_after_login’,10);
}

The code is VERY straightforward, but you might wonder how to get the post ID number. And, unfortunately finding that number is not as straightforward as it should be. So give that tutorial a read.

3. Remove Action

Now let’s also take a look at an example that uses remove_action() instead.

What would you do if you wanted to remove product images from WooCommerce?

Finding that in code and removing it can be tedious. And you might end up deleting the wrong lines of code. That could even break your website.

Instead, you should unhook the images:

remove_action( ‘woocommerce_before_shop_loop_item_title’, ‘woocommerce_template_loop_product_thumbnail’ );

Conclusion

WordPress hooks play an IMPORTANT role in WordPress development.

They simplify our lives with built-in actions and filters that can add, modify or remove functionality and data.

Hooks are a VERY powerful tool that can save a lot of time for every WordPress developer out there when used properly.

FAQs About WordPress Hooks

What are WordPress Hooks?

WP Hooks are a powerful feature that allows developers to tap into and modify the core functionality of WordPress without changing the core files. They come in two types: Actions and Filters. Actions allow you to add or change WordPress functionality, while Filters allow you to modify data before it is sent to the database or the browser.

How do I add a custom function to a Hook?

To add a custom function to a Hook, you use the “add_action()” function for Action Hooks or the “add_filter()” function for Filter Hooks. You need to specify the hook’s name and your custom function’s name. Optionally, you can specify the priority and the number of arguments your function accepts.

Can I remove a function from a Hook?

Yes, you can remove a function from a Hook using the “remove_action()” or “remove_filter()” function. You need to specify the Hook from which you want to remove the function and the name of the function you want to remove. Matching the priority parameter with the one used in the “add_action()” or “add_filter()” call is important.

What is the difference between Action Hooks & Filter Hooks?

The main difference is in their purpose. Action Hooks are used to execute custom code at specific points during the execution of WordPress, allowing you to add or change functionality. Filter Hooks, on the other hand, are used to modify data, like text output or field values, before it is saved to the database or displayed to the user.

How can I create my custom Hook?

To create your custom Hook, you use the “do_action()” function for an Action Hook or the “apply_filters()” function for a Filter Hook. Place this function where you want the Hook triggered in your theme or plugin code. Then, other developers (or you in other parts of your code) can hook into your custom Hook using “add_action()” or “add_filter()”.

About Author

Ales has been in love with computers ever since he got Amiga 500 almost 30 years ago. He earned his bachelor’s degree in Computer science from Ljubljana University, Slovenia. In 2010 he co-founded PremiumCoding, a website that makes awesome WordPress themes that are also for sale on ThemeForest. In the past few years, he focused mainly on combining web design, code and photography into a seamless experience.