Using Wordpress Actions with PHP Closures

AuthorJez R. Emery
Date
2. September 2021
Time1 min read

Say we have this function

public function addAdditionalEditorFiles(string $param)
{
  // do something
}

Now in order to enqueue files to the Wordpress Admin area, we need to utilise an add_action call, however these normally only allow for usage without parameters. e.g.

add_action('enqueue_block_editor_assets', [$this, 'loadAdditionalEditorFiles']);

So how do we pass a custom parameter to this function call? Using Closures.

$param = 'something';
add_action('enqueue_block_editor_assets', function() use ($param) {
  $this->loadAdditionalEditorFiles($param);
});