The more customization that you do when building a WordPress site, the more you will find that you have a lot of functions hooked to actions and filters. Sometimes these functions might be quite simple, returning just a single line of text or replacing a single word. Is there an easy way to make this more compact?
Yes, there is! Here’s how:
Enter the Anonymous Function
PHP allows you to use functions without names known as anonymous functions. These are also referred to as closures and are often used as callback procedures. For our purposes, these can be very useful tools when setting up customizations for your WordPress installation.
If you have done much work customizing your WordPress install, you may already be familiar with how filter and action hooks work. But just in case, here’s a simple overview.
Hooks allow us to “hook” functions to specific places in WordPress’ execution (or a theme’s or plugin’s execution). These can be “actions” that do something or “filters” that change something.
While actions and filters serve slightly different purposes, their use is essentially the same. To “hook” something to one of these elements, use the function called “add_action” or “add_filter” with arguments for the hook name and the function you are hooking to it. (There are some other optional parameters, too, but that will not be discussed here.) It looks something like this:
In this example, you then have the function to go with it:
How to Use an Anonymous Function
A function needs a unique name or it will cause an error with the script. When you have thousands of functions in WordPress and various plugins, plus anything you are adding via custom functions in functions.php, avoiding naming collisions can be a challenge. Using anonymous function allows you to avoid that problem and put the function right inside the call to add_action or add_filter instead of the function name. That makes things a nice, tight package.
Here’s a simple example. The regular way, we will hook the excerpt_length filter to a custom function to return a custom excerpt length value:
Here is the same process using an anonymous function instead:
Look carefully at the above – do you notice how in the place of the function name in the add_filter() call, we have inserted the entire function without a name?
That packs things up nicely.
Now you don’t have to worry another excerpt_length_example() function creating a namespace conflict. It also makes things easier because it’s all packaged together.
When NOT to use Anonymous Functions
This is a great tool to use – in the correct environment. When should you NOT use anonymous function?
First, anonymous functions were introduced in PHP 5.3 and support for them is included in all subsequent versions. But guess what? WordPress’ minimum PHP version is STILL only requires PHP 5.2.4. Now, PHP 5.2 is no longer supported by the PHP project and any host running it is asking for security trouble, but keep in mind that it may be out there. And your anonymous functions won’t work for that.
Now, suppose you’re smart and you run current PHP. But you’re posting an example or something somewhere on the Internet for someone else to read. Don’t assume (1) they know what anonymous functions are and how/when to use them, and (2) that they are smart enough to keep PHP up-to-date like you. Either don’t post an anonymous function OR at least give some explanation.
What about building themes or plugins? If you are applying code that will be used in a broad sense, like a plugin, then it’s best to NOT use an anonymous function for that. WordPress has some functions to *remove* actions and filters, aptly names remove_action() and remove_filter(). If your plugin uses an anonymous function for a hook, then no one who uses that plugin will be able to remove an action or filter your plugin applies. That makes things less flexible and customizable, so best to stick to defining a callback function.
Similarly, if your function will be used by more than one filter/action, then you would probably want to name your function. That way more than one filter/action can use it.
The Rest is Up to You
Anonymous functions are a great tool – in the right time and place. Learning to use them can help you speed up your development and also help you prevent namespace collisions that would bring down your site.