Understanding and Using the WordPress Hook System | premiumwd
Development / WordPress

Understanding and Using the WordPress Hook System

by david attard on November 11, 2016

We’ve been covering quite a few WordPress web development topics recently, so you’ve probably already heard about hooks, actions, and filters.

These are an essential part of the using the event-driven architecture used by WordPress. And they are your path to creating your own custom “version” of WordPress to suit your needs.

In a nutshell, an event-driven architecture is a way of listening for events that change a particular state and then reacting accordingly. Your reaction, what you write, is how you can create the functionality you desire to extend the functions of WordPress.

As a very simple example, publishing a WordPress post is an example of an event (

) which changes a state.

This is the foundation of the extensibility of WordPress, which has made the CMS such a rich ecosystem, thriving with thousands of feature-rich themes and plugins. There is another fundamental reason why using WordPress hooks are critical. Using hooks, you can extend and build upon WordPress without the need to edit any of the core source code. Actually, let me rephrase that statement.

If anybody you hire for WordPress development (or work for) suggests a core change to the WordPress code, fire them on the spot.

Anybody, who is willing to break the upgradability of your WordPress website has no idea what they are talking about. If they had a grasp on the basics of security, they wouldn’t even think of doing that.

We’ve spoken about this before. Upgrading WordPress (and its themes and plugins) to the latest available version is one of the fundamentals of securing WordPress. Developers who change the core source code will leave your website a sitting duck.

If your hired developers are willing to take such a treacherous path, chances are they are not well-versed in the basics of coding either. It’s very likely they will wreak havoc with the code they write, leaving you (or the people you hire after them) to clean up the mess that they leave behind them.

On the other hand, if you use hooks and all of the other extensibility options WordPress gives you, the upgradability of your WordPress is assured.

That means, whenever a WordPress core update is released, upgrading your WordPress to the latest version won’t break any of your code. You don’t have to worry about the upgrade of your own custom development. As long as the hooks are not deprecated, your customizations will survive the upgrade.

More importantly, your WordPress is kept protected from any WordPress security vulnerability which has been secured through the latest WordPress core update.

Hooks, Actions or Filters? What’s What?

Hooks are basically events that happen during the execution of the functionality of WordPress, themes or plugins.

But why are they actually called hooks? Developers who are looking to extend or implement specific functionality can “hook” onto the event as soon as it happens and perform a specific action related to that event.

As WordPress is going through its execution phases, it “checks” to see whether any plugin (or theme) has registered a function to be executed at that point, and if it finds any, those functions are executed at that point. The WordPress codex, as part of the documentation of the WordPress API, explains Hooks, Actions and Filters quite nicely, but I’m supplementing this information of course.

There are two main types of hooks

  1. Actions (aka Action Hooks)
  2. Filters (aka Filter hooks)

In reality, these are quite similar in what they can achieve, but there are a few subtle differences.

The Difference Between Action Hooks and Filter Hooks

Let’s start with what’s the same before we discuss the differences. Both actions and filter hooks receive data through a number of parameters.

Actions and filters are also mostly able to do the same thing (if you had to take a look at the WordPress source code, you’ll see that the code functions that implement actions are just wrappers of the filter code functions).

There is though, one key difference between the two types of WordPress hooks. Filter hooks are required to return a value, action hooks do not. In essence, with a filter hook, the hooked function gets a specific value, it does its thing and then returns that a modified (or not) version of that value.

An action hook does not need to return a value.

Putting it in more practical terms, with a filter hook, you’re typically working on content, doing your “changes” and then returning that content. For example, as we’ll see below the

filter hook works on the post title, whilst the

works on the content of the post.

On the other hand, an action hook does something (which can work on data or just use the data) when an event happens. The

action hook triggers when a post is published.

When and How to Use an Action Hook

You should use an action hook if you want to:

  1. Inject HTML or other content into the response buffer
  2. Modify one or more global variables
  3. Modify the parameters passed to your hook function

The way to add an action hook is as follows:

where $hook would be the action you want to hook onto, whilst $function_to_add is the function you’ve written that will be executed when that hook is triggered.

We’ll see some real examples of doing this further on in this article.

How and When to Use a Filter Hook

You can use a filter when you need to do any of the above, but you also want / need to change the value of the data parameter.

In fact, it’s not just a want, it’s a requirement. You must return a value for the parameter if you are using a filter hook. The way to adding a filter hook is quite similar to the way of adding an action hook:

Where once again, 

 would be filter hook to use and 

is the function you’ll be writing.

You might also like...

Development / WordPress

5 Ways to Make Your WooCommerce Store Swift and Faster

Starting an online store with WooCommerce is pretty simple. You just need to select a domain, a hosting plan and…

Plugins / Tips & Tricks / WordPress

11 Amazing WordPress Plugins for 2017

Instead of writing a regular top ten piece, I decided to focus on WordPress plugins that you probably don’t know about….

Development / Tips & Tricks / WordPress

4 Effective Ways to Help Grow Your WordPress Blog

 Many bloggers misuse the marketing concept: “If you build it, they will come.” Content may be the primary building block…

No comments

Sorry, the comment form is closed at this time.

See why people are switching to use our products. See Features Get Started
+ +
%d bloggers like this: