I'm using the Wordpress boilerplate to develop a plugin. At some point, I wanted to make dynamic functions with some external parameters when using $this->loader->add_action() so I used an anonymous function... but it doesn't work. So I resolved to bypass the loader and use the Wordpress standard add_action() and it works! But I know it's not good practice. How to pass an anonymous function through the loader? I'd like to know if any of you have a idea of how to implement this with the loader.
What I tried
$this->loader->add_action("views_edit-{$post_type}", $plugin_admin, function($views) use ($plugin_admin, $post_type) { return $plugin_admin->some_function($views, $post_type); }); The error I get
Your PHP code changes were rolled back due to an error on line 959 of file wp-includes/plugin.php. Please fix and try saving again. Uncaught Error: Object of class Closure could not be converted to string in wp-includes/plugin.php:959 Stack trace: #0 wp-includes/class-wp-hook.php(74): _wp_filter_build_unique_id() #1 wp-includes/plugin.php(121): WP_Hook->add_filter() #2 wp-includes/plugin.php(399): add_filter() #3 wp-content/plugins/myplugin/includes/class-myplugin-loader.php(124): add_action() #4 wp-content/plugins/myplugin/includes/class-myplugin.php(266): MyPlugin_Loader->run() #5 wp-content/plugins/myplugin/myplugin.php(76): MyPlugin->run() #6 wp-content/plugins/myplugin/myplugin.php(79): run_myplugin() #7 wp-settings.php(418): include_once('/home/...') #8 wp-config.php(106): require_once('/home/...') #9 wp-load.php(50): require_once('/home/...') #10 wp-admin/admin.php(34): require_once('/home/...') #11 wp-admin/plugin-editor.php(10): require_once('/home/...') #12 {main} thrown What I used instead by bypassing the loader (it works)
add_action("views_edit-{$post_type}", function($views) use ($plugin_admin, $post_type) { return $plugin_admin->some_function($views, $post_type); });