We're creating a plugin where, for example, it shows a metabox on all post types. To kick things off, I'm using the Starter Kit.
<?php
namespace PLUGIN_NAME;
class Foo
{
public function __construct() {
add_action( 'admin_init', [ $this, 'wpd_add_meta_box' ] );
}
function wpd_add_meta_box() {
$screens = get_post_types();
foreach ( $screens as $screen ) {
add_meta_box(
'global-notice',
__( 'Global Box 💥', 'global-box' ),
'global_box_meta_box_callback',
$screen,
'advanced',
'high'
);
}
}
function global_box_meta_box_callback( $post ) {
echo "We're alive!"; // Never shows inside the metabox that is rendered.
}
}
On a post page, I see the metabox but without the callback function.
Moving the code inside the functions.php
, using add_action( 'admin_init','wpd_add_meta_box')
, everything works fine.
How to render a metabox's content from a plugin within a class? Thanks.
global_box_meta_box_callback()
is a class method, so you need to use[ $this, 'global_box_meta_box_callback' ]
when supplying the class method toadd_meta_box()
.