Many of my WordPress developer counterparts come from a long background of non-development related work. Most of which have only been introduced to PHP as a programming language in order to better understand how their WordPress site functions. With this in mind you tend to find a lot of redundancy and, in many cases, unnecessary function associations.

Not many PHP developers find good uses for anonymous functions but they do come in handy when we look at developing more complex WordPress solutions with do_action and add_action.

The do_action function in WordPress essentially pulls in all associated functions from add_action and calls them using the PHP function call_user_func.  This function can use a reference to a function or an anonymous function. Using the later will look very familiar to you if you’ve ever done a lot of Javascript programming.

In most cases WordPress developers will create an unecessary function to reference in add_action like below:

do_action('example_action');

add_action('example_action', 'example_function');

function example_function(){
    echo "Testing Example";
}

The proposed streamlined approach is to pass an anonymous function and cut down on the number of methods created:

do_action('example_action');

add_action('example_action', function(){
    echo "Testing Example";
});

Hopefully this inspires more widespread use among the WordPress development community.

tags: / / / / / /

5 comments

  • Brando

    Posted: Jan 13, 2011

    You’ve been able to do this (with a different syntax, mind you) since PHP4.

    http://php.net/manual/en/function.create-function.php

  • darcyclarke

    Posted: Jan 13, 2011

    @brando My point is more that the use of anonymous functions within the WordPress development community has been limited for no reason. Time after time I see methods created for no reason other then to be used with the “add_action” function when an anonymous function would suffice.

    I did know that the use of these functions have been available in PHP for some time, as you mentioned. That said, their use and understanding needs to carry over to this community.

  • Orin Zebest

    Posted: Feb 1, 2011

    Actually, I found a very good reason not to use them. If you check out the PHP.net page on anonymous functions you’ll find an easily-overlooked note: “Anonymous functions are available since PHP 5.3.0.” Which, of course, is perfectly fine for many people, but a hefty percentage of WordPress installs run on servers < 5.3. Not only that, but if you use them in this way on a non-compatible server they simply fail silently. Super suck.

    I do wish there were a more backwards-compatible way to do things but I've yet to find one that works as elegantly.

  • darcyclarke

    Posted: Feb 1, 2011

    Good point Orin. Hopefully with WordPress dropping support for PHP 4 we’ll see an increase in installs running more recent versions of PHP. The very fact that there are books out about PHP 6 makes you wonder how long hosting providers and software providers can continue to support < 5.3 versions of PHP.

    For backwards compatibility, I guess we're left with the ugly use of named functions.

  • Zane Matthew

    Posted: Jan 10, 2012

    I do this from time to time, I think it’s awesome, yes there’s pro’s and con’s. As usual its a case by case basis.

"w3fools.com - because friends don't let friends use w3schools" ~ dan heberden
themify
Learning JavaScript