The example functions we used on both cases worked right away for us, they didn´t need us to know about those apparently weird things called “WordPress Hooks” so, why do we need to deal with them now?
A good answer to that question is that WordPress hooks will allow us to make our functions do many more cool things exactly when we want, and with a great flexibility. So, we better learn how to take advantage of WordPress hooks.
What are hooks needed for?
Maybe the first thing we should make clear is that WordPress hooks allow us to change the way WordPress normally behaves (or the way our theme or a plugin behaves, for that matter), making it possible to add new funcionalities or to modify the default functionalities when and where needed, without having to modify the WordPress core files or the original files of the theme or plugin which functionality we have modified.
And not having to modify the core files means something very interesting: during an update we will never lose the custom functionalities we added to our site. That is also the main advantage of using child themes, remember?.
As a matter of fact we will talk about WordPress hooks in the context of child themes, although everything we will learn here will be valid not only for them but also for WordPress theme and plugin development in general.
So what is a WordPress Hook?
A WordPress Hook can be regarded as a means to mark a particular point, moment or event in the WordPress code for which we may want WordPress to do something. They can be thought of as references or placeholders for the code to know when to do that particular “something”.
So a hook could be considered as a sort of “when” condition, like in the following sentences:
“when WordPress generates the content for a post, add my custom author bio box at the end of it”.
“when posts are to be presented on an archive page, truncate the content text to 500 characters”.
Of course, if we changed the core WordPress files we could make WordPress do whatever custom functionality we wanted. But besides the danger of conflicts or possibly security issues, all our custom code would be overwritten during the next update to the newest version of WordPress. That´s why I want to insist on the fact that a very important point about WordPress hooks is that they allow us to modify some parts of WordPress, a theme or a plugin without modifying the original files.
Types of WordPress hooks
WordPress hooks can be of two kinds:
The difference between actions and filters lies in the way in which that “something” we want our code to do is implemented:
- An Action is a function that is executed at specific points throughout the WordPress Core. It calls another function to do the job (for example to display a message to the visitor at the end of a post).
- A Filter is a function that modifies the value of something whenever the hook is triggered. In other words, the job in this case consists of changing something, and that job is accomplished by the Filter (for example to change the way the text is formatted on a post).
The final result of an action is some kind of output (for example displaying the message at the end of the post).
The final result of a Filter is not usually an output. The filter returns something that would be outputted anyway, but modifies it before (for example by truncating the text of the post).
Let´s not get confused by WordPress Hooks
Sometimes the same goal could be accomplished with either an action or a filter. This should not lead to confusion though, just think for example that adding a line of text at the end of a post can be done either by using an action that will output that line or by a filter that modifies the content by appending the line at the end.
But you should be aware of the fact that the terminology used by the WordPress community when talking about hooks is somewhat confusing. Even what I wrote some lines above about hooks being of two different types (actions and filters) may lead to some confusion. To get these concepts right, I´ve extracted and simplified some sentences from the WordPress Codex Glossary:
In technical and strict terms a Hook is an event that triggers the action or filter functions previously hooked to it.
Hooks are required by Actions and Filters, that is why the phrases “Action Hooks” and “Filter Hooks” are used.
WordPress Codex and source code comments sometimes confuse the terms actions/filters and hooks.
Actions, Filters and Hooks are also occasionally referred to as “action/filter hooks” or “action/filter/hook functions”.
Please don’t get overwhelmed. What I´m trying to point out here is that, strictly speaking, the hook is the event that triggers the action or the filter, it´s not the action or the filter itself.
However, the terminology I used when I asserted that a hook can be either an action or a filter is widely used throughout the WordPress community. In fact it is what you will hear or read most of the time, so we won´t be any different and won´t try to swim against the tide.
Accordingly, we will talk about actions and filters as hooks, but always keeping in mind that the hook is precisely what triggers the action or the filter.
Summarizing: if we call “H” the moment or event in the WP code to which we are hooking, the basic scheme of actions and filters behavior would follow these patterns:
Action –> When H happens, do this()
Filter –> When H happens, modify this() by doing that() to it
This post has been only a short introduction to WordPress hooks. In our next tutorials about hooks we will learn how to create our own actions and filters, and we will see some practical examples of doing cool things with them.
Meanwhile you can check the WordPress Codex if you´re interested in learning a bit more about hooks.
Other articles you may like:
Latest posts by Luis Alejandre (see all)
- Child Theme Detection Just Got Better - 17 January, 2017
- Child Themes: Enqueuing the parent theme stylesheet instead of using @import - 31 December, 2016
- Add functions to your WordPress child theme (5) – WordPress Filters - 8 August, 2016