Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
FREELessons:15Length:1.6 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 The Plugin API

Hello and welcome back to this Tuts+ course on WordPress plugin development. In this part of the course, we're going to look at the plugin API. And that consists of the filter and action hooks that are provided by WordPress. And you can also add to those by creating your own filters and actions. So here, the plugins API page on the Codex details what actions and filters are, and actions and filters, do two different jobs. An action hook is an empty hook which is either one of these actions provided by WordPress and there's loads more. In fact if I go here, and I click on Action Reference, so here we go, we've got all of the action hooks provided by WordPress. And this lists them in the order that they run, so you can identify which ones you need to use at different stages in the process of loading your site. And you see there are hundreds of them, and there are some that you'll use a lot more than others. And generally, if you're using a function and you look that up in the Codex, you'll find that that gives you information about what hook you need to hook function to that includes that WordPress function. So these are the action hooks, now what an action hook is, is a completely empty hook that you can add your own code to. So it's a bit like those includes that I showed you in Woo commerce before. It's somewhere where you pull in code that's somewhere else. So in this case, it's an action hook provided by WordPress and it's pulling in code that you add to your plugin. So you would hook your functions to an action hook in order to get the code within your function to run at that particular point in the process, or that particular point in the code. A filter hook is slightly different, so let's just go back and look at the filters, Filter Reference, here we are. So again, there's hundreds of them and a filter is slightly different. A filter hook goes around existing code. So if there's some existing code, for example, some static text, or some sort of functionality. You can attach your own function to that filter hook in order to modify the code that's already in that filter. So essentially, you'll replace what's already within that filter with your own code. So, here is a Tuts+ guide to the difference between actions and filters. And here you got a definition of each of them, and some examples of them. So here I've got a function being called directly, we don't need to worry about that, and here we've got hooking a function to an action. Now, there are two functions that we use here. The first one, do_action, creates an action hook within your theme or your plugin. So you can register, you're federally registering a hook that you can then attach code to by hooking your functions to that action hook later on. So say, in my theme, I might have an action called compass_in_footer. And compass is the prefix I use on the parent theme that I've developed, the theme framework that I use for my own themes. So here, I've got an action hook called compass_in_footer that I'm adding to my theme in the footer file. And then if I want to put some code in that spot, I can create a plugin and then use add_action, with the first parameter being the name of that action hook and the second parameter being the name of my function. So that effectively adds all the code within my compass_colophon function, into this spot here in my footer file, and I can add as many as I want. And you can also prioritize them. So you can add, for example, here we've got 20, the default is 10 if you don't add a third parameter. But if I have 20, that will fire after any other functions that have been run with the default priority of 10. Or alternatively, I could use 5 or a lower number in order to run it first, and let's just scroll down to look at filters. So here is an example of how you create a filter. So instead of do filter, which just doesn't exist, you use the apply_filters function. So the apply_filters function has two parameters, the first is the name of the filter and then the second is the default code that's going to run unless somebody creates a function and hooks it to that filter. So if nobody creates a function hooked to that filter in a plugin or elsewhere in the theme, that means that this code within my theme will run. But if I want to modify that, I can create a function within my plugin or my functions file. And I can hook it to that, which I would do by scrolling down here, you see the example, and here I've used add_filter. And add_filter works in the same way as add_action. The first parameter is the filter hook that I'm adding it to and the second is my function. So my function here is new_credits and this here is the name of the actual filter hook. So that would replace any code in my filter hook in the second parameter with the code in my new function here. Let's take a look at some examples in some of the Tuts+ courses that have already been created. So here we've got a course called 3 More Practical Projects to Learn WordPress Pugin Development, and that takes you through the process of creating three plugins. So within this there's a plugin called tutsplus-section-menus. And there's also a theme called tutsplus-plugin-development, and that's been developed specifically for that course. Now here in the theme, in my sidebar file, I added an action hook by using do_action tutsplus_sidebar. So I've now got an action hook right in that spot called tutsplus_sidebar. And in my plugin, I've added a function here, tutsplus_section_menu, and that here is hooked to tutsplus_sidebar. So my first parameter is the name of the hook, and my second parameter is the name of the function which you can find up here. So that puts all of this code here, it effectively outputs this code in this particular spot in my sidebar file. And I haven't added a priority, because this is the only function I'm hooking to that, and I just want it to fire with a default priority of 10. But if I was writing multiple functions, I could use priorities to make sure I knew which one was going to fire first. Now let's take a quick look at woocommerce, because woocommerce is full of hooks. So here is an example, add_action admin_footer, this is hooking to the action that's provided by WordPress. That's not a WooCommerce action, because all the WooCommerce actions have the prefix woocommerce. Let's see if we can find another one. Here's another one. So we've got a function here called wc_cleanup_logs. And that's being hooked to the woocommerce_cleanup_logs action hook. If I wanted to find out where the action hook was, so I know exactly where that function is gonna be firing. I could interrogate the WooCommerce documentation and find out exactly where it is. And these hooks are good example of what's called an extensible plugin or extensible theme. Because if you use hooks to add content and to add code, instead of just directly coding that into the theme template files, or into your plugin file, it means that other people can come along and modify or override the code that you've already added. So they could attach another function to that action hook. They could override what's in the filter hook by using add_filter. Or alternatively, they can unhook existing functions from the action and filter hooks. And let me show you how that's done. So if we go back into this tutorial about action hooks and filter hooks. Here you can see that there are two functions that you can use to unhook a function from an action. There's remove_action, and there's remove_all_actions. remove_action has two parameters, the name of the hook and the name of the function that's hooked to it that you want to unhook. So that function effectively won't go to that point in the code. Now the priority is only important if there's already a priority been assigned when it was originally hooked to the action using the add_action function, so you would use the same priority. And if it has got priority, you need to add that again. remove_all_actions, for that same hook, removes any existing actions from that action hook. So that means that your fresh code, that you're then gonna hook to it will be the only code that runs on it. Filters are very similar. If I scroll down here, you can see that you can unhook a function from a filter. There's only one which is remove_filter, there isn't a remove all filters. So remove_filter and once again, the first parameter is the name of the filter hook. And the second parameter is the name of the function that you want to remove from it, so that works in the same way as remove action. So if you want to completely alter the code that's being output by a third party theme or by a third party plugin, you can write your own plugin and use remove_action or remove_filter to remove that code, and then use add_action or add_filter to add your own. So that's an overview of the WordPress Plugins API. In particular, looking at actions and filters. In the next part of the course, we're going to start coding a plugin. And I'll show you the introductory text, that commented out text at the top that you have to use in order to set up your plugin and make it work properly. See you next time and thanks for watching.

Back to the top