Skip to content
Advertisement

Passing a class, method and arguments to WordPress functions

When adding pages to the WordPress admin you use add_menu_page, which accepts a callable function/method.

JavaScript

My issue is, I’m a little stumped on how to pass an argument to bar when it accepts/expects parameters, for example:

JavaScript

I’ve tried a couple different approaches but I can’t seem to get it to work:

JavaScript

So looking at line 287 of that file, it’s using call_user_func_array and I think it seems as if it’s possible to pass an argument in the $function parameter of add_menu_page but I just cannot get it to work:

JavaScript

Help would be appreciated!

Advertisement

Answer

You should be able to pass an anonymous function instead, whose body would simply call the bar method with the appropriate arguments.

The anonymous function would look like this:

JavaScript

Or alternatively, if you’re using PHP 7.4+:

JavaScript

So, just pass that as the callback, like this:

JavaScript

Note: I’m very unfamiliar with WordPress so this might not be the most appropriate way to do this.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement