Build Your Own Business Website header image

Whoops, you've found some premium content!

Watch the opening clip of this video to preview it,
the full video is available to paid members.

Part 4 – Example #1 – The “Hello World” of Action Hooks

Difficulty Level -

Filed Under Topics - ,

Listed Under Lesson Subjects -

Applies to -

Now that you have an understanding of what Action Hooks are and how they work, it’s time to start using them. In our first example we’re going to look at the “Hello World” of action hooks. We’re going to dive right in and start writing the code.

What’s a Statement?

This actually starts off with a statement. A statement is a single unit of instruction, statement is “do this thing”. And so the first statement we’re going to use is echo which essentially prints to the screen. So, echo ‘Hello World’.

Notice that it’s showing me as having an error here because it wants me to put the closing semicolon (;) in. This is a statement. It is a PHP function or directive and the instructions for it.

This statement says print to the screen the text inside of these two single quotes. That is echo ‘Hello World’. And each statement must end with a semicolon (;) that tells the server it’s the end of that statement.

Simple to Complex Statements

You can make statements very very complex. I’ll show you an example here of a complex statement in a plugin that I’m just about finished with for Genesis. If such and such then such and such and this is a if it’s not empty $instance [‘title’] and if not empty $instance[‘show_widget_title’] then echo the $args ‘before_title’] and apply_filters [‘widget_title’, $instance[‘title’].

There is the end of the statement, it started here and ended back there. When statements get long and complicated you need to be able to identify the end of the statement and that’s what that is, it’s the end of the statement.

What’s a Function?

We also have functions. A function is a collection of statements that gets executed in sequence, the top one first and the last one last. The function has a name and it has to be called from somewhere.

Create a Function

So we’re going to create a function and it starts off with function and then the function name and I’m going to say byob_hello_world.

After the function name comes the opening and closing parenthesis. Then after those comes an opening curly brace and then after all the functions comes the closing curly brace. So here i’ll be function byob_hello_world and then we’re going to insert here.

This is a function and this function doesn’t happen until it’s called or in a terminology that we’ve been using so far, this function doesn’t happen until it’s added to an action hook. So what we’re going to do is we’re going to add this function to an action hook.

Action Hooks Can Add or Remove

The action hook has essentially two potential actions – either add action or remove action. The add action adds a function to the hook and remove action removes a function from the hook. So if you want to take something away that you know exists you can use the remove action to take it away which you’re going to see plenty up here.

Create an Add Action Statement

So we’re going to create an add action statement, it’s add_action and then opening and closing parenthesis. And this is a function and it needs a semicolon after it.

First Argument/Add Hook Name

Then the first thing in here is the name of the hook. Let’s go ahead and look at action hooks in that Genesis Visual Hooks Guide to get the right name. I’m going to add that to genesis_after_header. So we’ll say add_action ‘genesis_after_header’. That’s the first argument.

Second Argument/Add Function Name

The second argument is the name of the function that we’re going to add which is ‘byob_hello_world’. And in its simplest form, this is an add_action statement.

Priority Argument

Now, this can actually take two other arguments. One of them you might use sometime and that’s priority. You could actually have a hundred different functions all set to run at the same time on a hook. WordPress has this idea of priority that allows you to change when the function runs.

So, if you need to make sure your function runs after everybody else’s is done, you would give it a higher priority. If you need to make sure it runs before everybody else starts you give it a low priority.

Order Functions Run In without Priority Added

Most functions have no priority specified which means that it has a priority of 10 and they fire simply in the order in which WordPress finds them so it may even seem random. If you need to control when your function happens in a sequence then you would use this priority.

Number of Arguments a Function is Passing Argument

Finally, there’s one other argument and that is the number of arguments that the function that you’re adding is passing. But it’s going to be like 2 or 3 years of programming time before you ever need to really even understand how that works.

So for all intents and purposes, you’re going to be working with 3 arguments at most. Really, mostly all you need is the hook name and the function name.

Upload the Changes to the Site

So let’s save that and upload it to our site. Let’s load this. See, there it is. It says Hello World.


Now, we could always add some HTML to this. For example, we could give it an h1 tag. This will echo any kind of text, any kind of text string whatsoever. Now it comes out as an h1. If you look at it in the HTML, there it is.

That is how action hooks work at their very simplest. This is, of course, not particularly practical so we’re going to go ahead and comment out this add_action so it doesn’t fire. Next up we are going to do something more practical. We’re going to change the position of this menu.

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
Subscription Options
0 Comments… add one
0 comments… add one