When WordPress filters gave me my a-ha moment

There are few feelings more gratifying than that moment when after weeks, months or even years of studying something, it just somehow clicks. Like a old friend, or a book you read several dozen times, it seems that it was always there and always will be.

One of the most powerful and confusing parts of the WordPress universe is hooks – action hooks and filter hooks. There are several outstanding tutorials out there which are well worth a good read or two. Which I did: I faithfully cycled through many tutorials on WordPress hooks, tried the code samples, and scratched my head in frustration.

Until that day when I was skimming through someone’s plugin code on GitHub and suddenly it clicked. I would’ve even hugged a potted plant if there were one in my office at the time.

Rather than go through the entire saga of WordPress hooks, I am going to share the one single point that was my a-ha moment.

Your own a-ha moments as you learn new things will be different. However, it is a good idea to narrow your field of inquiry, find the tiniest example and then drill it over again.

This post will only look at filters and will save actions for a different post. Most tutorials say that you should use the add_filter command in your functions.php file or in your custom plugin to get very cool results. And they are right. But why?

Let’s instead start with the apply_filters command. This command creates the filter and waits for someone to come along and hook onto it. The resulting code looks at first like gobbledygook:

Clear all that out of the way in your head and simplify it like this:

That is really all that’s happening. It is a variable assignment. After that assignment, it runs the variable through all of the other functions that were hooked to it. Like this one:

And now let’s tack that function onto our hook:

What happened here? The filter called some_filter assigned ‘David’ to the variable $my_name. Then it rattled down the list of all the other functions that hooked onto it using add_filter. Here, only one function hooked onto it, called add_my_last_name.

Because we are “filtering” (i.e. modifying) the variable $my_name, it is sent as an argument to the function. Inside the function, the argument is called $name. The function changes $name any way it wants to and returns it. (If the function returns nothing, then WordPress will print nothing. This is sometimes a desirable thing.)

Now the variable $my_name changed from “David” to “David Kissinger”. We can get further down the rabbit hole by discussing additional arguments to the filter. In our case, a second argument called “$last_name” would abstract the function further and make the code more sturdy and reusable.

Simplify the code to make it easier to see

A chunk of more clever code like this now starts to make sense:

At its most basic level this is equivalent to:

In other words, apply_filters can stand in place of any variable. Why would you use filters? So that other developers – and your future self – can make changes to data without modifying any code.

This is a fundamental point not just of WordPress but also good programming guidelines: Better to extend the code rather than change it. This is the ‘O’ in S.O.L.I.D. – software should be open for extension but closed for modification.

And that’s when it snapped, when it all came together. The maddeningly complex API for hooks and plugins just got simpler.

David Kissinger consults with companies, trade associations, non-profits and entrepreneurs on government affairs, public relations, marketing, IT strategy, web development and content.

Leave a Reply