Build Your Own Business Website header image

How to Specify Priority in an add_action Statement

Difficulty Level -

Filed Under Topics -

Listed Under Lesson Subjects -

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.

In this session we show how to specify priority in an add_action statement. We also demonstrate a working example of this and the purposes of using priority and how it works.

Video Transcript

Member: Here’s… let me just lay it out for you, typical you know, add action Thesis hook footer and then there’s a little function I’ve created or someone did called footer text. I think I got this from DIY themes. And then after that… okay so I’ve haven’t seen that many times. Add action,
remove action, Thesis hook and then the function and the parenthesis, seen that dozens of times, no big deal. But then at the end of this one, in the parenthesis, it’s Thesis hook footer then it’s footer text. That’s their thing for doing like the copyright in the footer. But then there’s a comma and then in quotation marks, it’s 99 and then it closes the parenthesis for the add action. And I’m like, what’s that all about? What’s the 99 stand for?

Rick: The 99 stands for the order in which this thing should be processed. Now it doesn’t need quotation marks.

Member: Yeah, it’s single quotations.

Rick: Yeah no, I mean it doesn’t need single quotations either or does it? Let me just double check the…

Member: Yeah okay, that’s a good point, Rick, because you mentioned that on Thesis Responsive how the standard is to move away from the… but this is the old code that I copied from the DIY Theme site maybe like a year ago or something….

Rick: Yeah so let’s see, so we’ve got an add action and so it takes 4 arguments. It takes the tag which is the hook. It takes the name of the function which you are calling footer text.

Member: Correct.

Rick: Which is probably not a good function name, right? It’s not unique enough. It takes a priority and it takes an accepted argument. And yeah, that’s right. See, the priority and the added arguments are integers. And so they shouldn’t be enclosed in single quotation marks. WordPress may properly interpret it anyway but it hides the fact… you put them in quotation marks, it hides the fact that they’re integers and that’s the only thing that can go after this. So save post, save my post and then you have 10 which is the priority and 2 which is the number of arguments that save my post takes.

Member: Right.

Rick: So priority, if you don’t specify a priority then priority is automatically 10 and each thing fires in order, essentially first come first served because they all have the same priority. Now if you have lower priority than 10, that is essentially a higher priority. It’s going to fire sooner. And if you have a higher priority number than 10, it’s going to fire later. And so somebody puts it in that 99, they want to make sure that their add action happens after everybody else’s stuff.

Member: Gotcha.

Rick: Now in something like you’re working on, you wouldn’t need to specify a priority. And in something like this here and you probably won’t encounter anything on my site that tells you that you need to specify the number of arguments. And so almost always, your add action is just the name of the hook and the name of the function. And then sometimes, if you go to try to specify that you want it to happen before or after then you change its priority.

I’ll give you a working example of this. I just finished writing a plugin for Genesis. And what it does is it adds a sidebar to… notice how this Genesis homepage has no place for text. It’s got these little widget areas. It’s got the slideshow, it’s got little stuff here. This is actually not very good for SEO. So I wrote this plugin that adds a sidebar before these widgets and after these widgets. And so this plugin, Agency Home Content includes and its home HTML. So what I am doing here is I’m saying… so if the person has chosen to show the upper content area that is if show upper content equals 1 then add action, Genesis loop such and such HTML or add this sidebar. If somebody has specified that they want the lower content then add this action to the Genesis loop such and such sidebar and I gave it a priority of 11.

Now the reason why this happens to work is because I’ve hooked… and that is the reason why this comes first because this Genesis loop is where those widget areas are hooked. So everything is hooked to the same hook. There’s a hook right here and each one of these things is hooked to that hook. So now I’ve hooked something to it before and hooked something to it after. So, all of these things are on the same hook, 1, 2, 3, 4, 5 sidebars are going to be on the same hook. These ones are following after my first one because I specified adding it at a hook that comes before Agency child theme hooks it. Agency child theme hooks this layer than template redirect so I hooked this function at template redirect which means this one is going to fire before agency does all by its lonesome. And if I wanted to make sure they did, I would say 9. Agency doesn’t have a priority specified so it’s automatically 10. And for sure, I want this one to be after any agency sidebars so I gave this one a priority of 11.

Member: Interesting. I thought that the number, that priority number just had to do with the time that it gets executed. But you’re saying that the actual order that it appears on the page.

Rick: Well, the order that this…

Member: Or both?

Rick: It is the order in which this thing is fired. This BYOB gaht upper sidebar HTML that is this function right here is going to fire, because it’s a 9, it’s going to fire before the one that Genesis specifies, because Genesis classifies it as a 10 or doesn’t specify it so it’s a 10. This one’s going to automatically act after. Now if something else came along and had a thing of 12, this would go in front of it. This would happen before it.

Member: Right.

Rick: So that’s how priority works.

Member: Great. And then for instance, the one that I was talking about, this is a good example because it shows the importance of using the priority number whereas in… when I’m creating a footer, whether it’s 1 or 95, it’s going to get hooked to the footer and it’s going to be exactly where it is the same time every time.

Rick: Well, if you do it at 95 and somebody doesn’t do it at anything, there’s something that’s going to land first, that’s going to go first because you said 95. So you deliberately said you want to be after everybody.

Member: Yeah but I’m saying if there’s no other hook in the footer…

Rick: Well, if there’s no other function hooked to the footer then you’re right. If there’s no other function hooked to the hook except the one you have, priority doesn’t matter.

Member: Gotcha, gotcha. And when they put priority number 9, I’m thinking that what they’re saying is they want everything else to execute on the page and this will be the last to fire like you’re saying.

Rick: No, 9 is…

Member: 99.

Rick: Oh 99, yes. That’s what they’re saying. Well, it’s not the last thing on the page because it’s actually in the footer. So what they’re really saying is if somebody comes along and adds other stuff to the footer, make sure this stuff happens afterwards.

Member: Oh okay. So that… so what I just did is I went to Thesis theme, okay and look at their footer widget, their fat footer widgetized footer and I just did this for demonstration site. And then I was wondering how it knew… and I also did the Thesis themes but I didn’t use… the code that you used for the footer is a lot more sophisticated and that’s what I used on the client’s site. But just for demonstration site, I used the Thesis one and code. So I used their code for the fat footer and then their code for the copyright in the footer. And the copyright came in the bottom because
of that 99 you’re saying.

Rick: Right.

Member: So there were 2 things hooked, 2 functions hooked to the Thesis hook and that’s how I control and make sure that that always ends up on the bottom, the copyright and the attribution, I guess.

Rick: Yes. By specifying the priority.

Member: Right, okay so that has a lot more meaning to me now because otherwise, I was just like, what’s the point you know, of sticking that in?

Rick: Well, this kind of thing shows up… well before I understood how priority worked, I would… if I wanted you know, one thing to be above another, I would remove the original thing and then, in order, put them back in in the order I wanted them. But you don’t really have to do it that way. All you have to do is specify a priority and it will shake itself out.

0 Comments… add one

Save $200 on Membership Now!

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

Leave a Comment