WordPress Development: Hooks (Actions and Filters)
If you’ve spent some time developing themes or plugins in WordPress, then you may have heard about hooks. In fact, it’s incredibly likely that you’ve used them, whether you know it or not.
In this article, we’re going to walk through a few things about hooks: what they are, why we use them, and how we use them.
What are hooks?
Think of hooks like placeholders. They’re inserted at meaningful places in code execution and allow you to “hook” your own function into that place of execution.
There are two different types of hooks: actions and filters.
It’s important to distinguish between the two:
Actions do something. Whether it’s printing out something in a certain spot, or taking some information and handling it, actions do something.
Filters are slightly different. They take some information, transform it, and send it back in another form.
Hooks can be accessed by including the code to do so in your theme or in a custom plugin that you have created. They help you make changes to layouts, change the functionality of plugins or even the core functionality of WordPress.
Why should I use them?
WordPress hooks are extremely useful. And not just WordPress core — any well-written WordPress plugin has a multitude of actions and filters.
They allow for so much customization that you can use them to do practically anything you need to do in WordPress. Plus, they keep your template files clean and allow you to customize without modifying those template files.
Not only can you use hooks that other people have written, but you can write your own hooks. Add them into your themes or plugins to allow extension of your code.
How do I use them?
WordPress has built-in functions that both 1) perform an action or a filter, and 2) allow you to hook your own function into an action or filter.
The ‘add_action’ and ‘add_filter’ functions are the most important to get a handle on first. These are the functions that take a custom function and make it run when you need it to. On the other side of things, there are also the ‘do_action’ and ‘apply_filter’ functions.
These functions all take a string value that connects them. So if within your theme, you wanted to allow anyone to do something after the body opening tag, you might add an action called ‘after_body_open.’ This would allow anyone to assign their custom functions to ‘after_body_open’ and their code would run every time that body tag opens.
For more detail on using actions and filters, you can visit the WordPress plugin API here: https://codex.Wordpress.org/Plugin_API
Things to remember
Here are a few things to keep in mind when using actions or filters (and with WordPress development in general):
- Avoid name collisions. Always prefix your function names or the names of hooks you create with something unique.
- Names of hooks should represent what they do or allow.
- Look at the source code (of a plugin, theme, or WordPress itself). A useful hook you didn’t know about may already exist.
- Filters should change data. They always need to return a value.
- Actions should do something.
WordPress has many built-in hooks. This provides a starting point for plugins and themes to begin expanding the WordPress core.
Below are links to reference for all of the built-in WordPress filters and actions.
Filter Reference – https://codex.Wordpress.org/Plugin_API/Filter_Reference
Action Reference – https://codex.Wordpress.org/Plugin_API/Action_Reference