Simple Callbacks in WordPress
Callbacks are the core of WordPress. The Plugin API functions rely on them directly, as do shortcodes! In WordPress core, we commonly see these referred to with strings which refer to function names:
Another common usage is to reference a method
Sometimes the return value is so trivial that creating a function or method just to return it is almost silly. The most common cases have been given their own full-fledged functions as part of WordPress core since v3.0:
But what if you wanted to return something else without creating a dedicated named function or class method?
The next option would be an anonymous function. These are possible even in PHP 4 using
create_function() but it uses
eval() internally and the usage is totally gross, so I would recommend never using that. However, unless your host is running PHP 5.2 (the bare minimum for WordPress - in which case you should run away or upgrade IMMEDIATELY), you can use a real anonymous function, like so:
Pretty nice, eh? Not a good solution if you want your callback to be able to be removed using
remove_filter but we’re assuming this is something you’re in control of here, and we wouldn’t need to do that. Read: if you are distributing a plugin/theme to be used by others, you shouldn’t do this.
So that’s really nice and all, but still more characters than necessary, especially if you want to do this kind of thing several times.
Let’s DRY all that up with a simple
__return helper function!
Just as shown before, we can pass a closure directly to the action or filter callback we are adding. The only difference here is we’re abstracting the creation of the closure within our little higher order function that simply returns whatever we gave it. The function isn’t called until the hook is fired.
Now we can write a single, expressive line of code to return whatever we want:
This will return “cool stuff”. Since we’re using a function to create our callback, we can pass any valid expression to it that we want to use to pass the result.
A practical example might be a simple shortcode for rendering the current year: