Now what we’re going to do is take a look at our example number 1. And example number 1 is the “Hello World” of action hooks. If you don’t know what I mean by “Hello World”, traditionally this is the very first command a programmer learns.
Almost every introduction to any programming language has some sort of “Hello World” as its first project. Ours is the process of moving the Thesis theme nav menu from above the header to below the header.
Before we get too involved in that, we’re going to talk a little bit about terminology because PHP does have terminology and it is useful to be able to understand that terminology.
The first thing we want to talk about is a statement. A PHP statement is a single unit of instruction and it ends with a semi-colon. So this is an example of a PHP statement. It’s echo Hello World; That is one PHP statement.
This is another example of a PHP statement. It takes the value of 1, adds it to the value of 2 and places that in a variable called sum. So sum = 1 + 2 if you echoed sum, you would see 3. This takes this arithmetic value of 1 + 2 and places it in the variable sum. And each one of these are PHP statements. They are a single unit of instruction. PHP knows that unit of instruction has ended because it finds a semi-colon.
On the other hand, a PHP function is a collection of statements. And that collection of statements is executed in a sequence from the first one to the last one or from the top of the page to the bottom of the page. So when you write a function, your first PHP statement is the first thing you want PHP to do and the second one is the second thing you want it to do. You place them in a sequential order inside of that function.
Every function has to have a name. Statements don’t have names. Statements are just statements but a function has a name and in order for that function to be executed, it has to be called. That is, some place in the process of executing statements, that function has to be called or it’s just going to sit there. It’s not going to do anything. It doesn’t happen automatically.
The function only is executed when it is called and we will see what I mean by that here in a second. And in fact, this is how we are going to call our function in the first place and that’s using an action statement.
An action statement, this is a WordPress function that calls a PHP function when a hook is fired. And there are 2 action statements available to you. There is the add action and the remove action.
An action statement has a specific syntax. So you have the add action and then the parenthesis around the parameters of that add action. The first parameter is the hook name. WordPress names its hooks. The Thesis theme names its hooks so that you know where the hook is.
And so the very first parameter is the hook name surrounded by single quotes. Then the second parameter is the function name that is the name of the function that you’ve written. So at this hook, you want this function to fire. These two parameters are separated by a comma. Your third parameter is a priority.
If you’ve played around with this a little bit before, you may not ever seen this priority number. It actually happens routinely that there are a whole bunch of add actions to any given hook. And so WordPress includes this priority system so that you can tell it where in the sequence of action statements you want yours to be executed.
If the priority is never listed then the default value is 10 and what happens is WordPress automatically assigns the priority of 10 to all functions unless a priority is otherwise assigned. And so then what happens is the action statements are activated in the order in which they are found.
You have no control over the order in which they are found. However, you can supersede the order in which they are found by using this priority. Now more often than not, you’re not going to use a priority. You’re only going to use a priority in select situations because 10 usually is good enough. I am going to show you an example of where we use a priority today.
And then we have one final parameter which is not really shown here called accepted arguments and we’re going to ignore that entirely. You don’t want to describe the add action or the remove action without acknowledging that there is a fourth parameter called accepted arguments. But it’s not a parameter, it’s an advanced parameter. It’s not one that you’re going to use at this point in your career and it’s far too complicated to discuss at the beginning level. So we’re going to ignore it.
As a practical matter, most of your add action and remove action statements will only have the hook name and the function name.
Write the Code to Remove the Thesis Nav Menu
So let’s go ahead and do one because now we’re here to “Hello World”. The first part of this example is to remove the Thesis theme nav menu. Okay so we start off with a remove action so it’s remove_action and then parenthesis and then single quotes. And the Thesis nav menu is hooked to the Thesis hook before header.
So if we look at this visual guide here, Phil represents the Thesis hook before header after the Thesis hook first nav menu item. But in fact, in sequence, Thesis hook before header happens. The Thesis nav menu is hooked to that and so really, conceptually, these two things should be reversed.
But nevertheless, Thesis hook before header is where the nav menu fires and the name of this function is Thesis nav menu. So following our example, the hook name is thesis_hook_before_header. Notice there are no spaces here. We’ve just got underscores under here. Now we put another comma in and then single quotes.
See the red line here? This is telling me that I’ve got a syntax error. That’s because I haven’t finished writing the line of code. Once I put my semi-colon in there then it believes this is finished.
The first one is the hook name. The second one is the function name and that is thesis_nav_menu. Okay and this is an action statement. It is removing the Thesis nav menu from this hook.
So we’re just going to test that. We’re going to save it over here to FileZilla, hook up to the right website… CTLP Lesson 9 and then go to wp content, themes, theis_184, custom. This is the custom functions php file that I am editing right now. So I’m going to upload that to my website.
This is the website we’re working on so all of our edits are happening here. And right now, it’s showing what it’ll look like with the original custom functions php file but when I refresh this, you’ll see what it looks like with the second. And now, that Thesis nav menu has been removed because we have completely removed the Thesis nav menu. It simply isn’t there anymore.
Write the Code to Add the Thesis Nav Menu to a New Location
The other half of Hello World is the add action. Let’s just put that semi-colon in there right away. Okay, add action and now the hook that we want to use is Thesis hook after header and then Thesis nav menu. Okay so we’ve removed it and now we’re going to add it. It’s exactly the same thing. We’re adding versus removing. We’re taking the function, Thesis nav menu and we’re adding it to the hook, Thesis hook after header. If we upload that to our site and refresh it, now our menu is placed below the header.