Join Carrie Dils for an in-depth discussion in this video Action hooks explained, part of Advanced WordPress: Action and Filter Hooks.
- [Instructor] We've already learned that WordPress hooks come in two flavors: actions and filters. In this lesson we're going to take a look specifically at action hooks, what they are, and how we can use them. So if we scroll down this plugin API page a bit we see these Actions Functions. And these are the functions that WordPress gives us to work with actions. I want to highlight the most common ones you'll see here. First we've got has_action() which checks to see if any action has been registered for a particular hook. Then we've got add_action() which let's us hook a function onto a specific action.
Do_action() which basically lets you drop a bookmark in the code where you can come back and hook actions to later. And then the other one here that I want to point out is remove_action(). It does the exact opposite of add_action() as the name suggests and lets you remove an action from a specified hook. So these are our gateway functions, so to speak, in order to work with action hooks. Let's go ahead up here to this action reference and take a look at what we've got. A quick scroll down this page and you can see that there are tons of action hooks baked into WordPress that are available for us to use.
Here for example we've got the actions run during a typical admin page request. That means that if you're logged into WP Admin and land on any page within the admin then these are the things happening in the code, the various hooks that WordPress gives us to interact with. All of these actions are called with that do_action function. Note that these aren't listed alphabetically here. They're listed in the approximate order in which they fire. So on the left side we have the action hook and in the right column we've got some notes about what's happening and that's just for an admin page.
If you explore this page here you'll find the entirety of action hooks available in WordPress. So I mentioned that all of those are called in WordPress with this do_action function. Let's go ahead and look at the syntax for that. So we've got do_action, our open parentheses, and then we're looking for a tag which is going to be the name of our action hook and then we've got some arguments that we can pass to it. And if we go on to read the description we can see that the tag or the name of the action hook is required.
However those additional arguments are optional. I think it's always easier to see code versus read about code so let's go find some do_action at work. Here I've got the WordPress code base I'm using on my local WordPress installation. I'm using Sublime Text to look at this but you can use whatever text editor you prefer. I'm going to do a quick search here and I want to look specifically for a function called login_header.
And here come up the search results for that do_action( 'login_header' ) and if I double click on this it'll actually take me to the file where that code appears and that's this WP login page. So this is an example of do_action in the wild. We've got do_action and then we're just calling the name of that function login_header. So this is an example of where we could come back with some custom code and attach an action to this spot in the code using the add_action function. So let's go ahead and take a look at the syntax for using add_action.
We've got the add_action, open parentheses, and the tag which is the function we're hooking on to, in this case it's going to be that login_header, and next we've got the function that we actually want to hook, and then we've got optional priorities and arguments and the developer reference here provides us with some additional information we could use to research more on what each of those are. I've got my own version of WordPress running locally and I've created a really simple plugin that we're going to use as a base to test something out.
So I'm going to go ahead and login to my WordPress admin and if I go to Plugins, you can see that I've got this plugin named Hooks Test activated. So if we go back over to Sublime Text I'm going to navigate to my Plugins folder, find that Hooks Test plugin and open up that file and right here all we have is the very minimum requirements of a WordPress plugin. If you want to learn more about the basics of creating a plugin check out this course on WordPress, Understanding Custom Plugins with PHP.
So going back to our code, first I want to write a function that outputs Hello world to the screen. So there's my shell for the function and I'm simply going to echo Hello world. Now right now if I saved this and went and refreshed my WordPress admin page absolutely nothing would change on this page and that's because we haven't hooked into WordPress yet. All we've done is write our own function.
So let's go ahead and hook it. So I'm going to use the add_action and I'm attaching it to that login_header function and the function I'm attaching is my own function, hello_world. So going back over here to WP login, and we can see here that that fires in the login page header after the body tag is opened. So if I go back to my WordPress admin page here and refresh it, what I'm hoping to see is that Hello world right on the top of the page.
So if I refresh, there it is. And if we use our browser's Inspect Element tool we can see that that's literally happening right after the opening body tag. So this isn't really a practical example but hopefully it demonstrates how we can use action hooks in WordPress to run our custom code.
- Actions and filters explained
- Identifying available hooks and filters
- Looking at load order
- Understanding callback functions
- Creating custom hooks
- Using third-party hooks
- Building a new WordPress plugin with filters and actions