wpthemedetector-logo
WordPress Theme Detector Blog
WPHooks

Add functions to your WordPress child theme (3) – Introduction to WordPress Hooks

15 comments

So far in this tutorial series we have seen how to add a new function to our WordPress child theme, either by modifying an existing function or by creating a brand new one from scratch.

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:

  • Actions
  • Filters

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

What´s next?

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.

Luis Alejandre

Luis is the creator of the WPThemeDetector tool and the chief editor of this blog. You can read more about Luis in our About page.
Liked this post? Please share it!

15 Comments

  1. Thanks for the great intro! I am lookig forward to hear more about hooks and filters etc

    • Hi Susanne,

      Sorry. I know it is taking too much time for the next post of this series to get published, but you´ll soon know why! 😉

      • You have a fantastic way of explaining things… very clear and easy to follow. I have tried to understand actions and filters many times and never been able to really grasp it. Thanks to this piece, I feel I finally see the big picture, so muchas gracias y por favor escriba mas!

        • Thanks a lot for your encouraging words, Bill.

          I promise I´ll come back to this tutorial series as soon as I am finished with something that is eating up most of my time.

  2. Thanks for the tutorial.Looking forward to know how to create hooks!

  3. Great post!
    Waiting too for next post regarding to hooks
    But why don’t you make your them responsive 🙁
    I have to keep page scrolling left right up down for reading posts here!

  4. Very clear information. I’ve wanted to know more about child themes and Luis, you helped clear up some of the technical aspects of the backend that will help me working with my themes.

  5. I am very new to wordpress and created a child theme for one of my job site. The problem is their are many css file available.

    the main css file is in the theme root directory and some other files in Styles folder.

    Now I also want to change a file in the style folder.

    So my question is, do I need to create a styles folder in the child theme and then put that file in that folder or do i need to put that file in the child root.

    waiting for reply.
    thanks

  6. Well explained post. I was able to add some functions on my site. Thanks for sharing

  7. Thanks…Today I have learned something new. Hope to learn some more new stuff!!

  8. nice article,
    I also want to know more about hooks in the wordpress

  9. In my local Nigeria pidgin English… I’ll say “Luiz u be baba!”

    Meaning you’re simply great!

    Thanks again

Trackbacks/Pingbacks

  1. L’Hebdo WordPress n°202 : WordCamp Europe 2013 – WordPress 3.7 – Traduction | WordPress Francophone - […] A la découverte des hooks (en) dans WordPress et comment les utiliser. […]
  2. WordPress Francophone : L’Hebdo WordPress n°202 : WordCamp Europe 2013 – WordPress 3.7 – Traduction - WordPress Actualités : WordPress Actualités - […] A la découverte des hooks (en) dans WordPress et comment les utiliser. […]
  3. L’Hebdo WordPress n°202 : WordCamp Europe 2013 – WordPress 3.7 – Traduction - […] A la découverte des hooks (en) dans WordPress et comment les utiliser. […]
  4. I finally did a child theme - […] i just did my first child theme. . Thanks to Vantage Child Theme tutorial and this article from WP…

Submit a Comment

Your email address will not be published. Required fields are marked *