7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.2 Connect PHP Functions to Hooks

In this lesson, I'll show you how to use action and filter hooks—including your own custom PHP hooks and built-in hooks. You'll also see how to control your code when writing PHP for WordPress by using pluggable functions and function priority.

Related Links

4.2 Connect PHP Functions to Hooks

Hello, and welcome back to this Tuts+ course on learning PHP for WordPress. In this part of the course, we are continuing with functions. And you may remember that in the previous part, we wrote some functions. So there were these two simple functions, the second of which was plugable using that if statement there. Now I've added another function to my file which register as a widget. And I'll be using that to demonstrate how you can hook into an existing action hook in WordPress in order to register your widget, or in order to run a function in general. Because there are a number of ways that you can run functions in WordPress, and that's what this part of the course is about. So let's just go back to the Codex in WordPress. So here you've got details of the actions and filters that you can use to hook your functions to. And that's probably the most common way of running a function within WordPress. So here, for example, we've got the action reference which lists all of the action hooks that are available in WordPress. So there are two types of hooks. Action hooks and filter hooks. And they work slightly differently. Action hooks are empty. So if you add a function to an action hook, it will be the only thing that happens at that point. So it'll add something new to that point in the code where the action hook is located. Filter hooks aren't empty. A filter hook surrounds some existing text or content, or code. And by attaching a function to a filter hook, what you'll do is replace that existing content with your new content. So it means that when you're coding a theme or a plugin, you can add some default text for example, wrap it in a filter and then you can override that with another function if you want to in the future. And this tutorial here on Tuts Plus goes through the difference between actions and filters in detail. And we've also got a tutorial where the beginners go to actions and filters which shows you in detail exactly how you code them. Now let me demonstrate some of that in my file. So the first thing I'm going to do is take this function here to register widgets. And I'm gonna hook it to an action hook that's already provided by WordPress. And I do that by using the add_action function. So add_action has two parameters. The first one is the name of the action hook that you're hooking your function to, and the second one is the name of your function. So in this case, because I'm registering a widget, I need to hook it to the widgets in its hook. And then my second parameter is the name of my function. And I'm going to copy and paste that so I can be sure it's correct. Make sure I put semicolon at the end of my line of code and that's how I run this function here. So without hooking it to any actions, it won't actually do anything. Your functions, they just sit in your functions file or in your plugin files, but until you hook them to an action or you call them within your theme, they won't actually work. So, on these ones here I might not want to hook those two and the existing hook in WordPress. What I can do instead is create hooks within my theme. So to do that, the first thing I need to do is identify the hook in my theme. And let me show you the header file in the theme that powers my WordPress site. So here there are quite a few examples of do_action. And what do_action does is create a hook at that point in the code. So if I then run any function and hook it to the compass in header hook, it will be output at that point in my header file. So it's as if I added that code to my header file. So if we go back to this function here, rachelmcc_say_hello, if I want to hook that to my header file, let's say after the header, I've got a compass after header hook here. I'm gonna copy the title of that and then I'm gonna type add_action My first parameter is the name of the hook. And my second parameter is the name of my function. So what that will do is if I've created a functions file or include file in my theme here, that would output Hello World at this point in my header. Another way that you can run this function at a point in your theme is by calling the function directly. So here my function is called rachelmcc_say_hello. I can copy that and paste it directly into my theme. And what that will do is run that function. Now I'm gonna comment that, And that means I know what that's gonna be doing. I could also comment my do_action function and add to the comment there the functions that I've hooked to it. So every time I add a new one I could go into my header file and write a comment just to let anybody know who's coming in and editing in the future what's been hooked to it, so they can edit it if they need to. I'm gonna delete this because I don't want that in my file, but that as you can see is two ways to run that function at that point in the code. Now the advantage of using an action hook instead of directly typing that function name into your code, is that it gives you much more flexibility because you can run more than one function at that point. So let's say I wanted to write another function which would also be added to that action. So I'm gonna write a function which will output today's date. So that will echo out today's date. Now if I wanted to add that to the same hook, compass after header, I would use add_action again. And again, I'll copy my function name, And add it as the second parameter. What you now is two functions hooked to the same action hook, which is perfectly valid. The issue is that you might want to control what order they appear in. So let's say you want the date to appear before hello world. At the moment, you can control that using priority, which is the third parameter. So I'm gonna give this second function a priority of 10, which is the default priority. And I'll give the first one a priority of 20. So what that'll do is fire this function before this one. Now what I could do is leave this 10 out completely, because 10 is the default value for the priority. So if I just kept that blank and had that at 20, that would still fire after this one. So it's up to you whether you add that or not. I like to just so I know exactly what's going on. But I know that it's superflous. So if I add those two functions, hook them to compass_after_header and add those priority values after my header, the date will be output followed by the words hello world. And I can do the same thing with this pluggable function here. So let's copy this code here. Change the name of the function. And I've just realized there's an error with this function, I didn't copy across correctly from the last module. Right, so I can also add this one to an action. Now I'm gonna change its name to Grumpy. And then I'm going to copy my add_action line. Plus still compass after header. It's gonna be rachelmcc_grumpy as the second parameter. And I'm gonna give this a priority of 30, so that is output last. So now I'll have three things being output, one after the other, after my header. So that's how you run your functions using action hooks. You can also run them using filter hooks. And if we go back to the WordPress website, you can see that there's a list of filters on the codex as well. So in some cases, you'll find that a function that you need to run should be attached to a filter instead of an action hook. And to create a filter hook in your theme or your plugin, instead of using do_action to create an action hook, you use apply filters. And that will have two parameters, the first is the name of the filter hook and the second is the content. So let's go back to our demo file and I'll show you how that works. So here I've got apply filters, the name of the filter and the default content. So you might want to put that around for example a heading. You could use that in your theme for example, if you want to put latest post as a heading. But you wanted to give yourself the option or other users of your theme the option to override that at a later point, either in a plugin, or in the themes functions file. And then to override that, you would write a function and use add filter to hook your function to that filter. So let me show you how that works. Now here I'm replacing that content their, latest posts, with My Latest Posts. Now at the moment, because we're working in PHP, this wouldn't actually be output. So I need to add echo before apply filters. And you'll find the echo is often used with filters when you're adding your own filters. The filters that are already built in and WordPress don't necessarily have that. But I find filters particularly useful. Let's just separate this out so you can see what we're working on. I find filters particularly useful when it comes to echoing out content. So if I wanted to then make this function here work on that filter, I would have to use add filter. So the first parameter is the name of the filter, which is rmcc_new_filter. So this works in exactly the same way as add_action. The second one is my function name. So once that function has been hooked to the filter, what will be output instead of h3 latest posts is h3, my latest posts. So my latest posts in an h3 tag. So that's how you add a filter into your code, and how you hook a function to it, as well as adding actions, which we've already gone through. In the next part of this course, we're gonna be moving on to looking at PHP in the loop. See you next time, and thanks for watching.

Back to the top