Learn the basics of WordPress action and filter hooks. Action hooks enable you to integrate custom functionality. Filter hooks enable you to modify data as desired. Taken together, action and filter hooks play a key role in plugin development.
- [Narrator] In WordPress, hooks are central to plugin development. They enable us to interact with the WordPress core. Hooks are foundational to how WordPress operates, enabling us to modify, extend, and remove core functionality. They play a huge role in making WordPress very flexible and extensible. If you look through the plugin API, you'll see that it's all about hooks. There are two types of hooks, actions and filters. Action hooks enable us to run custom code at specific points during the execution of WordPress.
For example, we can use actions to do things like echo output, write to a file, or modify the database. Filter hooks enable us to modify data during the execution of WordPress. For example, we can use filters to modify post content before sending it to the user. To use either actions or filters, we need a callback function. Let's open the simple example plugin that we created in the previous Getting Started video. For example, let's add an easy callback function for an action hook.
Let's keep it simple and create a function that sends an email. Here we are using wp_mail which is a WordPress core function. We enter an email address, a subject, and a message. Because this function simply sends an email, we can use it with just about any action hook.
For example, we can register this function with the init hook. The syntax here is straightforward. The first parameter specifies the hook and the second parameter specifies the name of the callback function. Because this function is registered with the init hook, the email will be sent every time WordPress executes. The specific point in execution is determined by the hook that we are using.
In this case, the init hook fires very early. This diagram shows the order in which action hooks are executed. As you can see, the init hook happens very early in the process and there are many other hooks available. So, we can include our callback function at just about any point that's required. Now, let's look at an example of how to use a filter hook. For this callback function, we want to filter the content variable. The content variable is passed to the function here.
And here we will add some custom content to the content variable. It could be anything. And then we need to return the content variable like so. This callback function accepts the content variable and then appends some custom content and lastly returns the variable to WordPress.
This function is designed for use with the content filter hook so we can register the function like so. Here is the content filter hook. Again, the syntax here is simple. The first parameter specifies the hook and the second parameter specifies the callback function. When this function is registered with a content hook, our custom content will be appended to the content of every post.
Likewise, we can use other filter hooks to modify any data that is necessary. And WordPress provides many filter hooks as you can see here. This extensive collection of hooks enables us to customize many aspects of WordPress core functionality. As you may guess, it's possible to add our own custom hooks to WordPress plugins. Doing so is smart because it enables other developers to extend and customize our plugins. It's also possible to remove action and filter hooks whenever necessary.
So, it's possible to connect or disconnect just about anything. To learn more about WordPress hooks, check out the video course Advanced WordPress: Action and Filter Hooks. That should give you a solid understanding of WordPress hooks that will improve your development skills. Also check out the official documentation at WordPress.org.
- WordPress APIs
- Action and filter hooks
- Activating and deactivating plugins
- Plugin security
- Creating the directory and files
- Adding menus and the settings page
- Inserting custom functionality
- Testing and debugging WordPress plugins
- Creating widgets
- Managing users and roles
- Adding custom post types and taxonomies
- Working with custom fields and database queries
- Using APIs: Transients, HTTP, and REST