Understanding WordPress filters and actions

Whenever we’re hunting for solutions to our WordPress woes, there’s a chance that the answers we seek will contain code consisting of two components; a dedicated function, and an induced function: add_filter/add_action. Most of the time, we can just post the code at the end of our functions.php file and that will work fine; problem solved and nothing too complex to worry about. However, if you have a bit of understanding of how these functions work and what they do, you’ll not only have a better working knowledge of the code you’re pasting, but you’ll find it easier to find solutions to common problems.

actions-and-filters

Filter and actions

Filters and actions are one of the greatest things we developers received from WordPress – with them we can work miracles! But before that, we need to know how to distinguish them from each other so that we know when it is appropriate to use actions, and when we should use filters.

So what’s the difference between a filter and an action?

A filter is a function that modifies the data it receives, while an action is a function that is called at an appropriate time.

So how does this affect us in the real terms? Well, we will, for example, use filters when we’ve already got HTML code that we want to modify, and use actions when to create HTML code that we don’t currently have.

Filters can be implemented wherever the apply_filters function is used – a filter is created by calling the add_filter function.

The first argument of both functions is the name of the filter, which means that we can have an unlimited number of filters in WordPress, each identified by a unique name.

Actions, on the other hand, are called wherever the do_action function is used – the add_action function is used to add an action. As is the case with filters, both of these functions take the name used to identify the action as the first argument.

Applying our own filter

Filters can be used to, for example, modify the length of your posts’ shortcuts (excerpt):

The above excerpt_length filter will police every post shortcut, cutting it off after twenty words to keep your shortcuts uniform.

Calling actions

An easy example of when calling an action might be useful is adding text at the end of a theme’s structure by using the wp_footer action:

Why are filters and actions so important?

Filter and actions offer us a wider range of control without changing things at the core. We may modify plugins and their output without modifying the plugins code. WordPress does not have any native mechanisms for overwriting plugins the same way as you do other elements via child themes, so if we decide to update our plugin we’ll also lose any modifications we’ve implemented. However, if the plugin in question offers a wide range of filters and actions, then, we may easily modify it without worrying about the dangers of updating it (at least until changes in the plugin are so large that we need to add a modification to our code to make it compatible again).

Additionally, if our theme provides lots of filters and actions, then there is no need to overwrite as many files in the child theme. Instead, we can expand the functions.php file of a child theme with additional actions. Consequently, theme updates throw up far fewer issues, as there are only a small number of files that require modification on each release.

Summary

Filters and actions are one of the most important tools in developers’ hands – when used properly they offer a wealth of ways to solve many common issues, as well as offer a safer way to modify elements of WordPress that would otherwise require a change to the WordPress code itself; a much more dangerous prospect since modifying the source code is frowned upon except in some very specific cases, or not possible at all.

Share
This article was first published November 21st, 2014