Disclosure: This post contains affiliate links. I may receive compensation when you click on links to products in this post. For an explanation of my Advertising Policy, visit this page. Thanks for reading!
What are the WordPress add_filter vs add_action functions?
Ever get a plugin or theme that would be perfect if you could just make one or two tweaks to it? However, these two tweaks will require you just to add a little bit of code, maybe even some shortcode.
But before you do, you need to understand the basic concepts of add_filter vs add_action, the differences and how they work in WordPress before you turn an almost perfectly coded plugin or theme into a complete jumbled mess.
An “add_action” in WordPress is what you use to create a trigger “hook”. When something happens, then do-something-else.
An “add_filter” is used to “hook” data, i.e. change/replace. For example, where there is [some-code], change it to some-other-expanded-code.
The “hook” is a “function” or “method” for a specific filter or action. More on “hooks” below.
WordPress offers filter hooks to allow plugins to modify various types of internal data during its runtime. A plugin can modify data by binding a callback to a filter hook. When the filter is later applied, each bound callback is run in order of priority, and given the opportunity to modify a value by returning a new value.
- Actions are those PHP functions which execute the output.
- Filters are those PHP functions which return the output.
We can extend any plugin which uses the actions and filters without modifying their code. By adding filters and actions in our own theme or plugin. Let’s go into more about how these work in WordPress.
So, what exactly is a “Hook” in WordPress?
“Hooks are a way for one piece of code to interact/modify another piece of code at specific, pre-defined spots. They make up the foundation for how plugins and themes interact with WordPress Core, but they’re also used extensively by the WordPress Core itself.
There are two types of hooks: Actions and Filters. To use either, you need to write a custom function known as a “Callback”, and then register it with a WordPress hook for a specific action or filter.” Source: WordPress.org.
What’s the difference between hooks, filters and actions?
As mentioned above, “hooks” is basically the collective name for filters and actions. Both are meant to change the normal behaviour of functions.
Speaking from a programmatically point of view, there is no big difference, as you can see from the fact that in the WordPress source code adding an “action” is the same as adding a “filter”.
The difference is in how they are used. You use an “action” to completely change the behaviour of a function or to add something to an existing function.
You use a “filter” to change the outcome of a function or a variable.
What is the difference between do_action and add_action in WordPress?
A “do_action” registers an “action hook” while “add_action” adds a callback function to the registered hook.
What is the difference between Action Hooks and Filter Hooks?
Action Hooks
“Actions Hooks” are intended for use when WordPress’s core or some plugin or theme is giving you the opportunity to insert your code at a certain point within theirs to execute a specific task or “action”.
Filter Hooks
“Filter Hooks” behave very similar to “Action Hooks”, but their intended use is to receive a value and potentially return a modified version of the value.
A “Filter Hook” could also be used just like an “Action Hook”, i.e. to modify a global variable or generate some HTML, assuming that’s what you need to do when the hook is called.
What do filters do in WordPress?
A “filter” takes the information it receives, modifies it, and returns it. In other words, it filters something and passes it back to the hook for further use.
Therefore, a filter is used to modify something in a specific way so that the modification is then used by the code later on.
What do actions do in WordPress?
An “action” takes the information it receives, executes whatever instructions it is given in its coding, then returns nothing. Basically, it acts on something and then exits, returning nothing back to the calling hook.
An action interrupts the code flow to do something, and then returns back to the normal flow without modifying anything.
So where “filters” are sets of parameters or instructions, “actions” are the execution of instructions.
Can I use an add_action before add_filter when tweaking a WordPress plugin?
It basically doesn’t matter what the order you use the add_action() and add_filter().
What does matter is the order in which the corresponding do_action() and apply_filters() are called.
So, if “apply_filters”, for example, (‘wp_insert_post_data’) is run before the “do_action”(‘save_post’), then it’s not going to work. You’ll have to think of another way to get the results you’re after.
So what’s the real difference between add_filter hooks vs add_action hooks in WordPress?
In reality “Filter Hooks” are pretty much a superset of “Action Hooks”. The “Filter Hooks” can do anything the “Action Hooks” can and a bit more. Although the developer doesn’t have the responsibility to return a value with an “Action Hook” that they do with a “Filter Hook.”
“Filter hooks” basically define the parameters of a task, while “Action hooks” execute tasks. For more information on the differences between “add_filter” hooks and “add_action” hooks, check out this official WordPress documentation in the developer section on WordPress.org.