hooking

Hooks API from scratch – Part 2: About hooks; it’s not about the hook

It’s not the hook

There, I’ve said it. The whole Hooks API is not about the hooks. In this whole story of hooks, bags and boxes, none of them are the hero. Who could it be then?

(Here’s our standard contemplation pause.) What could it really be about?)

To answer this question, we must answer another question:

Why do we need the hooks?

To hang the bags. So, are we hanging the bags on the hooks or are we using the hooks to hang our bags on something else?

It’s the wall

The wall is our concern. We want to hang bags on the wall but can’t because it’s flat and smooth. We need to add some hooks to the wall so that we can hang our bags.

blankwall

Making a great wall

Imagine that people bought pre-made (readymade) walls from a store called The Wall Makers. The Wall Makers is a neat store that gives you walls at a very low cost. Also, it provides a lifetime of free upgrades. Every now and then, employees of the store come knocking on your door. They bring brand new upgraded walls and replace your earlier wall. This way your wall is always strong and new! The only condition is that you can’t drill holes or modify the wall itself.

wall-makers

So, what do you think would sell more, a plain smooth wall with no attachments or one with some hooks? Screws and nails are technically hooks since they let you add things (shelves, TV, paintings, etc) to the wall.

What should the store manufacture and why?

Hooking into the Wall

The Wall Makers are the numerous programmers that build the wall, I mean WordPress. Like smart people, they know that you and I want to hang paintings, add shelves, wi-fi routers, calendars and a lot of other things to the wall. Every person can customise the wall to their own needs by adding whatever they want using the hooks.

In the same way, WordPress comes with a lot of hooks all over it that let us customise it whatever way we want.

hooks in wp

There are two functions that are used to create hooks:

Hooking into WordPress

The $handle parameter is the identifier for the hook. This is what allows us to add our own functions using the add_action and add_filter functions to a hook, as described in the previous post.

hooking
WordPress will have a hook in the code somewhere, like this:

We’ll make our function run there in a manner similar to this:

What next?

In the next post we’ll understand how hooks work and what the difference between the two is.

Yet another exercise

If WordPress is a wall, what are themes? Let me know in the comments.

4 thoughts on “Hooks API from scratch – Part 2: About hooks; it’s not about the hook

  1. Pingback: Hooks API from scratch – Part 4: Action hooks in action | Hook, Refine and Tinker

Leave a Reply

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