Now that you understand what Action Hooks and Filter Hooks are, let’s go ahead and create a very first action hook. Here we are in master.php. And if you’ve worked in Thesis 1.8 before you’re going to see this is familiar because what we’re going to do is create a function essentially.
So, function and then you give the function name and I’m going to say, byob_hello_world, opening and closing parenthesis and then opening and closing brackets.
This is sort of the basis of our function, this is the function name. These brackets hold parameters that can be passed to the function. We’re not going to really talk about that today but that’s what that space is for and then the function is contained entirely inside of this set of brackets.
Now, it’s essential that your function names be unique so you wouldn’t ever want a function name that says “excerpt” because that is about as generic as it gets. And the chances are, it’s going to conflict with some other function name and those kinds of conflicts will crash your site.
You want to make sure that one, your function names are not generic but are very specific and two, they may as well be readable. You may as well be able to tell by reading the function name what it does.
Now, you’ll notice that in my writing, I preface everything with byob. I know that if I put byob in front of something, the only person who would likely have written something that would conflict with it would be me and so I pay attention to not do that.
I always use a unique prefix in front of each of my function names just to make sure that there’s no possibility of conflict either with WordPress or with some other plugin or something someone else has done, so the function name should be unique.
The next thing we’re going to do is do some kind of a directive. Well, the simple directive here is echo and then ‘Hello World’; the quintessential initial function for all beginning programmers.
Add the Action Hook
So, we’ve got our function here but nothing’s going to happen until we add it into the processing using the action hook. So that’s what we’re going to do now, add the action hook, add_action. We’re going to use a WordPress action hook so in parenthesis, ‘wp_head’ that is the name of the WordPress action hook and then the name of our function and then a closing semicolon.
This is a valid add action statement, it’s add_action and then the hook name and then the function name and there are two other parameters that can go along with this.
You can put the priority in and the priority is a number that tells WordPress essentially how to prioritize the location of this action. So if there are a whole bunch of things firing at the same time like they are at wp_head then the lower the number, the sooner it gets fired and the higher the number, the later it gets fired.
Now, if you don’t include a priority number, WordPress sets everything automatically to 10 and then it just happens procedurally. WordPress takes everything from 1 to 9 first and then everything else and then everything greater than 10 in order of the number. So you don’t have to specify this number here. And it does take one other which is the number of parameters but that’s an advanced concept that we’re not going to bother with.
Upload to the Site
We’re just going to leave it like this, add_action(‘wp_head’, ‘byob_hello_world’); okay? So now what we’ll do is upload that to our site and up to Thesis and master.php. And then let’s go to the site and you can see it says Vale Puella over here.
If we view the page source, here’s our head tag and you can see coming all the way down here Valle Puella is firing just after the meteor-slides/slideshow script and just before the WordPress admin bar CSS. So it’s firing right in there and it says Vale Puella; it’s echoed inside of the HTML head.
Change the Priority
Let’s do that one more time. We’ll change that to 99 and upload it. Now it’s going to come down below, so now Valle Puella is down here at the very bottom. Obviously the shortcut icon has been given a value of greater than 99 so Valle Puella is not at the very bottom but anyway, that’s how priority works in that equation.