Hooks API from scratch – Part 4: Action hooks in action

If you haven’t, please go through the previous post, Hooks API from scratch – Part 3: An experiential guide to action hooks. This part 4 won’t make complete sense without that.

Your function is to read

Let’s say you are a WordPress installation.

Yes, imagine you are a WordPress site!

Your job is to read the paragraph from Part 3. Since you are a WordPress website, you are created of code instead of flesh and blood. Every action of yours will be via functions. So, stand_up(), close_your_eyes(), dance(), etc are your functions.

actionaction

To read the paragraph, you run a function read($text) where $text is the paragraph. Here’s the imaginary read function:

These are imaginary functions for illustrating a concept. They have no practical use.

The code is explained in the inline comments. To add, the read function first breaks the text into an array of words. Then one by one, the word is passed to the say_aloud function.

Saying kakapo

I asked you to say kakapo whenever you come across WordPress in the paragraph. This idea has two parts:

  1. Say kakapo
  2. after ‘WordPress’

I can rewrite that as:

  1. Say kakapo
  2. after a word, if the word is WordPress

The first part is easy to represent:

But how do we check if a word just said was kakapo?

When we read, we take a pause after each word, don’t we?

From earlier posts, you know what that do_action is. You also know how we can add our say_kakapo function to this hook. Open a new text file on your computer and try writing the code, yourself. See Hooks API from scratch – Part 2: About hooks; it’s not about the hook for help. Avoid looking at the answer below till you have tried at least once.

Scroll a lot, after you’re done!

 

 

 

 

 

 

 

 

 

 

 

 

Here’s your answer:

Now say_kakpo will run when the action hook will run in read function. This is as good as writing:

Is everything fine till now?

I hope you’ve noticed that there’s a problem with the code so far. It will say kakapo after every word. We want it to happen only after WordPress. We can check if a word is WordPress, whatever the casing may be:

We can do this in the read function itself:

So now only when the word is WordPress, the action hook will run and you, the WordPress installation will say ‘kakapo’. However, now I’m going to change my instructions. I want you to say ‘kakapo’ after every the not WordPress.

.

Getting the hook’s context

Remember, read is your, i.e., WordPress’s function. We don’t want anyone and everyone to change things. So, we can’t run our hook like that if we want it to be flexible. So, the logic that checks what the word is should be inside the function added to the hook, say_kakapo.

The problem with this is that no one passed the $word to say_kakapo. We need to know what the word was. In other words, we need to know the context in which the hook was run. That is done by do_action by passing variables as parameters. It can pass any number of variables, second parameter onwards (the first is the handle, always).

Let’s pass $word as a parameter in do_action.

We can now access $word:

Now, everything shall work, as expected.

A little mental work out

Instead of asking you to say kakapo after a word, I am asking you to say it whenever you read ‘middle of’ in the paragraph. How would you write the say_kakapo function?

HintYou’ll need the word and the word just before the current word. The hook would then be do_action( 'pause_after_word', $word, $word_before );

Extra work out

How would you rewrite the read function so that the hook can pass the $word and $word_before as described in the hint above.

Leave a Reply

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