WordPress hooks 101

In this article I am going to explain WordPress hooks 101. WordPress hooks are areas inside code made available to you to add functionality or change values without physically changing the original software’s code. Code that is connected by hooks can also be disabled and replace by other code. With the use of hooks your code is also preserved in the case of a software updates to WordPress, WooCommerce, Plugins or the theme.

With this article I want you to grasp the logic on what a hook is and how it functions. I left out the other properties of hooks to be explained in another article to make this article as simple as possible.

WordPress hooks actually starts with the developers of WordPress, WooCommerce, Themes Frameworks and Plugins and how these developers implement hooks into their software so that other developers like you can bring on changes without effecting their code.

 

WordPress hooks basics

So what is a hook? A hook is a reserved space inside the code of a software program, put aside by the developer of the software, for you to add code or change values. So basically the software program will run from the top down executing each line of code as it goes down until it hits a line with a “hook”. Now it will check if someone is calling (using) this hook? If the hook is been called… is there any code attached to it? If so execute this code first before continuing with the normal program. If nothing is calling the hook then continue with the normal execution of the program.

For example.:

 

do_action() & apply_filter()

There are two types of hooks that you are going to use often.:

  • The one is a action hook.: do_action(); // This hook applies code.
  • The second one is a filter hook.: apply_filter(); // This hook allows a value (variable) to be modified.

apply_filter()

I am going to start with the difficult one first; apply_filter(). I will try to make it as easy as possible. I promise.

So you are building some application….a simple math calculation. You are feeding a function two values that return the sum of those values.

Simple hey? So you released your application to the world to use and all goes well until people start to contact you that the simple calculation of just adding two values together is a bit long in the tooth. They want to change the value that is returned (sum).

You do this buy adding a hook. More correctly a filter hook, because the developers that want to bring on changes needs to return that value to your software so that your software can return that value to the code that called it.

So you create  a filter hook with apply_filter(). You give it a unique name (for example.: name of the current function) and you pass the current value of $sum to the code that is calling or want to call this hook. You are essentially lending out your $sum value to someone and expects them to return it after they are done.

For example I added the line.: “apply_filter( ‘myCalulation’, $sum );

Now the developer that wants to use your value $sum and bring changes to it can do so by adding the following to their theme’s function file.

  1. They first need to create a link to the hook by using the add_filter() function call
  2. For the first arguments they need to pass the name of the hook they are connecting too.
  3. The Second argument is going to be the developer’s function name who wants to hook into your code and inject or change values with his “custom code”.

Think of this function call as a bridge or two electrical power point connections to connect to each other. That is basically what this function or the add_action() function does.

So the developer that is bringing on these changes to your $sum value is adding a VAT / TAX amount to it. After the VAT / Tax is added it returns the $sum variable to the function of your software.

 

Now from the start. Back to your application.

So when your software is calling the method “myCalculation” and passing it the two variables of 50 each

for example….

…you will receive a total of 114. So what happened here?

  1. The method myCalculation() is called. Its passed 2x arguments (values) of 50 each.
  2. Inside the plugin these two values are added up (sum) and it forms a total of 100.
  3. Before the total value is returned it is intercepted by a call to the hook from another source. “The anonymous software developers code”.
  4. It grabs that value of the variable $sum and passes it on to the new function that intercepted it.
  5. It adds a vat amount to that $sum variable and returns it to the plugins function. This is important.: It returns to the same location “line” where it was once before, before it was intercepted. The $sum variable has now been updated with a new value.
  6. Now it continues to the next line and returns the value to the function call and stores the value inside $total which now equals 114.

So now that your understand what a filter hook is I am going to show you what a action hook is.

do_action()

You access action hook to add code to a hook that was place (reserved) by the software developer for you to use. That is it! No filtering of any content. You are only adding code in a reserved spot to existing code that is busy executing.

For example you have created a custom function with custom code to hook into a maths function of a plugin.

The plugin.:

The output will be.:

This is a calculation of 1 + 1 = 2

 

That is the basics on hooks and I hope you enjoyed it. Please stay tuned for more articles on hooks. If you want to add value to this article please comment below.