The WordPress Plugin API enables you to write custom code that interactions with WordPress. The Plugin API is made up of hooks, also known as actions and filters, that let us hook into WordPress at a specific point in run-time, such as when a user logs in, or when someone leaves a comment. The process of developing a plugin is really the process of writing a function and registering it with the right hook.
- [Instructor] I've mentioned a couple of times that plugins can extend the core functionality of Word Press. In this lesson I'd like to look at how that's possible. Before we do that I wanna tell you a little about APIs. Which is short for application programming interface. At its most basic, an API is a set of rules that detail how one piece of software can communicate with another. To use a silly example, let's say that I have a locked car, and I want to access the inside of that car. Well there are rules about how I get into the car.
I have to use a key to unlock the door and then I can open the door up. I can't unlock the door with a banana. I can't even unlock the door with a house key. The rules of accessing the car dictate that I need to use a particular key. Of course this analogy starts to break down if the car's not yours and you're holding a baseball bat, but I digress. In programming terms, an API is what lets me access either send information to or get information from another piece of software. Word Press has a plugin API, and that provides a way for me to write a bit of code that communicates with Word Press.
The plugin API is made up of hooks. Of which there are two types. Action hooks and filter hooks, and these are what let us hook into Word Press. What do I mean by that? Well Word Press is code, right? When you log into your Word Press admin there's a certain bit of code that runs. When you publish a post, another bit of code runs. When someone leaves a comment, there's another bit still. For every action you can do with Word Press, there's code behind it that's making it happen. Hooks are probably the most foundational topic developers need to understand when it comes to building advanced functionality in Word Press.
Hooks are present throughout Word Press code, and that lets you latch onto any particular action that happens and run your code at that moment. This is incredibly powerful, because in the early days of Word Press, we couldn't do this. So if we wanted to change the default behavior we had to go in and literally hack the core code to suit our needs. When Word Press added hooks, it gave us the ability to access what's happening in the code and then layer in our custom functionality. For example, let's say that when a user logs in, instead of redirecting them to the default Word Press dashboard, you want to instead redirect them to the general settings page instead.
Well there's a hook there. You can write a function that says what URL you want to redirect them to, and then add your function into that hook. At run time, when Word Press executes that hook, and it's actually a filter called login_redirect, it'll trigger the function you attached to it that sends the user to the settings page. At its simplest, the process of plugin development is really about writing a function that does something, then finding the correct hook when you want to trigger your code, and then attaching your function to that hook.
And then of course there's doing this many times over. So here we are at the Word Press plugin API documentation. As a developer, the codex is really the user manual for Word Press, and there's all sorts of great information in here for developers. The plugin API is a great place to get started. It explains hooks, actions, and filters. What they are and it'll also give you some examples of actions and filters. We'll talk about the distinction between those a little later and dig into how to work with them.
In addition, Adam Brown at Brigham Young also has a database of all the Word Press hooks called the Word Press hooks database. This is pretty cool because it shows you what hooks are available for any previous version of Word Press. You'll also find a treasure trove of information on plugin development at the plugin developers handbook located here at WordPress.org. So the key to plugins is the hook. The process of developing a plugin is really just a process of writing a function and then attaching it to the right hook.
Curious what you can do with a plugin? Carrie covers some practical examples, including plugins for additional post types, custom taxonomies, and new admin features or layouts. Plus, get best practices for documenting and securing your plugin, and find out how to make your plugin accessible to others by internationalizing it or sharing it on WordPress.org.
- Setting up a local development environment
- Using plugins vs. themes
- Writing a simple plugin
- Working with the Plugin API
- Creating actions and hooks
- Documenting and securing your plugin
- Internationalizing plugins
- Hosting plugins in the WordPress repository