Anonymous PHP functions in WordPress hooks

Anonymous PHP functions, also known as closures or lambda functions, allow creating functions without specific names. In WP, they are most convenient as callback function values in hooks (filters and actions). They appeared in PHP starting from version 5.3 and were slightly improved in 5.4.

It turns out that they have been around for a long time, but if you look into the code of WordPress plugins, they are practically absent there. For example, in the same JavaScript, they are used everywhere. Why is that?

First, let's take a look at how a hook looks with a regular function and a closure:

// 1. Hook with a regular function
add_filter( 'the_content', 'filter_the_content');
function filter_the_content( $content ){
	// modify the content
	return $content;
}

// 2. Hook with an anonymous function
add_filter( 'the_content', function( $content ){
	// modify the content
	return $content;
});

The first method essentially has one advantage: since the function has a name, the hook can be removed if necessary using the remove_filter() or remove_action() functions. Therefore, anonymous functions are rare guests in plugins and themes. However, if a hook is created using a closure, it cannot be reliably removed.

Advantages of anonymous functions in WP

  • Short syntax (easier to write).

  • Cleaner global namespace and no need to worry about function name conflicts.

  • No need to come up with names for callback functions (sometimes it takes a lot of time).

Disadvantages of anonymous functions in WP

  • It is impossible to remove a hook created based on anonymous functions.

  • It is impossible to use the same function in multiple hooks - a separate anonymous function must be written for each. For example:

    add_filter( 'the_content', 'filter_the_content' );
    add_filter( 'widget_text', 'filter_the_content' );
    
    function filter_the_content( $content ){
    	// Modify the content
    	return $content;
    }
    
    // or
    add_filter('the_content', function( $content ){
    	// do something
    	return $content;
    });
    
    add_filter('widget_text', function( $content ){
    	// do something
    	return $content;
    });

    However, this drawback is ambiguous because in PHP (since version 5.3) and in JS, an anonymous function can be placed in a variable and then used as a callback function for a hook:

    $my_callback = function( $content ){
    	// do something
    	return $content;
    };
    
    add_filter( 'the_content', $my_callback );
    add_filter( 'widget_text', $my_callback );

    But this approach is logical, very rarely, almost never... It can be applied, for example, in a separate theme template file, when the script is already finishing its work and obviously such hooks will not interfere with any other code. Or when they are triggered only under some rare condition - in a separate branch of the code logic. In general, I don't remember when I needed it...

Using "use" in anonymous functions

Another feature of lambda functions is the ability to use variables from the current scope, using the "use" operator:

$var = 'Hello world!';
$func = function() use ( $var ) { echo $var; };
$func(); //> Hello world!

Variables are passed as values, but you can also pass a reference to the variable by specifying &:

$var = 'Hello world!';
$func = function() use ( & $var ) { $var = $var .' We are visiting!'; };
$func(); // call
echo $var; //> Hello world! We are visiting!

When to use anonymous functions in WordPress?

  • If the code is not intended for widespread use.

  • The ability to remove the callback function is not needed and will not be needed.

  • No need to support PHP versions below 5.3.

Now, after understanding what is what, we can answer the question, "Why are anonymous functions not used in WP?" - Because WP code should be as flexible as possible, and closures are not about that...

Read more about anonymous PHP functions in the documentation.