I am trying to pass a variable to the callback function through add_meta_box
. I know that there is $callback_args
that can be used to pass the variable. But somehow I just can’t get it to work within a class. This is my code:
class myclass{
//...blah blah...adding action
function add_ex_field() {
add_meta_box(
'apextadmin',
__( 'My Custom Field', 'myplugin_textdomain'),
array($this,'the_callback_function'),
post,
//the $param is some value that is saved in my option table.
array('test' => $param)
);
}
//do I need to include these two parameters? because they gave me error!
function the_callback_function($post, $metabox){
echo '<h1>testing field</h1>';
echo $metabox['args']['test'];
}
}
global $myclass;
$myclass = new myclass();
Thanks.
Sometimes an example says more than a thousand words. Here’s a small plugin that will guide you:
If you’re wondering, how I identified those hooks that are used for the static
init
function to load, then take a look at my »current admin info« plugin here.The simplest way to do this is, just remember the arguments being passed on a subset of the original array. While you are using a class structure, the basic non-class methodology still works.
So to pass an argument within the add_meta_box call, would be like this:
Check it out on:
https://developer.wordpress.org/reference/functions/add_meta_box/