$metabox
is not in the global scope, that is why your two functions is not working. If you turn debug on you will get a a lot of debugging notice error messages regarding the use of $metabox
. Simply doing the following
$metabox = new Metabox;
doesn’t make a variable global. It need to be defined as a global. But before you go on and do this, take into consideration that it is recommended to stay out of the global scope, specially with an easy-to-recreate variable like $metabox
.
Just think about this, you have two plugins using the same variable $metabox
. This works great. Now, you define $metabox
as a global in your theme. This changes the values for all instances of $metabox
which leads to your plugins using wrong data and failing in what it should do.
PROPER SOLUTION
Create yourself a “global function” which you can reuse, something like
function metabox_global_function()
{
$metabox = new Metabox;
return $metabox;
}
Then you can use metabox_global_function();
anywhere in any function.