Understanding WordPress Hooks

When I started working on WordPress, I remember that one of the most confusing terms was the WordPress hooks. Probably this is something pretty simple for you and probably not. However, the essential thing I will try is to explain how it works and why it is crucial to understand.

What is a WordPress Hook?

Hooks in WordPress essentially allow you to change or add code without editing core files. They are used extensively throughout WordPress and Plugins/Themes and are very useful for developers. You can learn more in the official documentation right here.

To keep it simple let me give you an example. Imagine that we are editing a post and when we click the “Update” button, WordPress behind the scenes runs a lot of code to make it possible, and every code run code where we can add extra actions if we need it or, we can change the value that should return a function in WordPress. Before continuing with more examples let’s learn the two kinds of WordPress hooks that we have available.

Actions and Filters.

There are two kinds of WordPress Hooks. Actions and Filters. An action hook is basically the way to add code at a specific moment that a code is executing. For example when a post was updated or when a user was updated. A filter hook is a way that WordPress let us modify the value that a function or a variable should return. For example, if we want to add extra content to every post content, we could use a filter hook to do that.

The Anatomy of an Action Hook.

Basically, that’s the way how we can hook into an action hook. But probably this is a bit confusing for you know. So, let’s see the next example.

Imagine that we would like to notify a collection of emails that a post was updated. To be able to do this we could use a hook named post_updated . WordPress will fires this action hook every time that a post has been updated. So, let’s hook to that action and add our code.

Where should I add this code to make it works? If you are working from your theme you should add it in your functions.php or any file that was loaded in the functions.php of your theme. You can paste this code and test it.

Using action hooks we can hook any code to WordPress. Probably you are asking now. How can I know what’s the hook name that should I use to do specific tasks? Good question! This is the most complicated part of this. You will need to google it first or you could use some helpful links like https://adambrown.info/p/wp_hooks/hook/actions.

It’s important to know that action and filter hooks are present on any environment of WordPress. It means that a Plugin or a Theme has hooks. If you need to hook an action to a plugin or theme, you should check the plugin/theme documentation and find the hooks that it uses. For example, if you are using WooCommerce, you can find a full hook list here https://woocommerce.github.io/code-reference/hooks/hooks.html

The Filter Hook Anatomy.

The filter hook anatomy is basically the same of an action hook. The difference is that a filter hook uses the function name add_filter instead of add_action. And another difference is that when we use a filter hook we always have to return a value and the value that we have to return is the first param received otherwise it will break our code. Something important to know about filter hooks is that we could receive more than one argument, but only the first argument that we can modify and return.

Let’s code an example of how to use it. Imagine that we would like to add footer content to every post content. To be able to do this we should filter the WordPress hook named the_content. So, let’s hook it.

So, copy this code in your functions.php and test it 🙂

If you liked this post please share it!