Closures and arrow functions in WordPress hooks
Anonymous functions are a convenient way to write a short hook callback right where it is added. PHP has had closures since 5.3 and arrow functions (fn() =>) since 7.4. In WordPress they come with two things to know: how variables from the surrounding code get into them, and the fact that a closure cannot be removed with remove_filter() by anybody else.
Problem: A plugin adds a the_content filter as a closure that uses a configuration variable, and another plugin, or a later change in the same plugin, cannot remove that filter.
Solution: Pass outside variables with use (or rely on the automatic capture of arrow functions), keep a reference to closures you may need to remove, and use named functions or methods for callbacks that other code should be able to remove.
Capturing variables
<?php
$prefix = 'ha';
// A closure sees outside variables only through use (copied at definition time)
add_filter( 'body_class', function ( $classes ) use ( $prefix ) {
$classes[] = $prefix . '-active';
return $classes;
} );
// An arrow function captures used variables automatically, also by value
add_filter( 'body_class', fn( $classes ) => array_merge( $classes, array( $prefix . '-js' ) ) );
The value is copied when the closure is created. If $prefix changes later, the closure still has the old value. To share a variable that changes, capture it by reference (use ( &$count )); arrow functions cannot do that.
An arrow function is a single expression. As soon as the callback needs an if, a loop or several statements, use a normal closure or a named function.
$this in classes
A closure defined inside a method can use $this:
<?php
class HA_Notice {
private $option = 'ha_notice_text';
public function register() {
add_action( 'wp_footer', function () {
$text = get_option( $this->option );
if ( $text ) {
echo '<div class="ha-notice">' . esc_html( $text ) . '</div>';
}
} );
}
}
( new HA_Notice() )->register();
Removing a closure
remove_filter() needs the same callable that was added. A new closure with the same code is a different object, so this does nothing:
<?php
add_filter( 'the_content', function ( $c ) { return $c . '<p>Thanks!</p>'; } );
remove_filter( 'the_content', function ( $c ) { return $c . '<p>Thanks!</p>'; } ); // removes nothing
Keep the closure in a variable or property if you need to remove it yourself:
<?php
class HA_Content_Wrapper {
private static $filter;
public static function enable() {
self::$filter = function ( $content ) {
return '<div class="ha-wrap">' . $content . '</div>';
};
add_filter( 'the_content', self::$filter, 20 );
}
public static function disable() {
remove_filter( 'the_content', self::$filter, 20 );
}
}
Other plugins and themes have no access to that variable. For callbacks that a child theme or another plugin might want to remove, use a named function ('ha_wrap_content') or a method of an object you make reachable; how removing works in general is explained in removing actions and filters.
When to use what
- A few lines, used once, nobody needs to remove it: a closure or an arrow function is fine.
- Code in a theme or plugin that others build on: a named function or a method.
- The same callback on several hooks: a named function, so it exists once.
NOTE: Arrow functions need PHP 7.4. In a plugin that still supports older PHP versions, a single fn causes a parse error that takes down the whole site, not just the plugin. Check the minimum PHP version you promise in readme.txt before using newer syntax.