[ad_1]
[ad_2]
Hi,
what do you feel is better style or better practice to hook into WP actions?
Using dynamic Classes/Methods to hook into actions or using static Classes/Methods?
Version A, dynamically:
new MetaboxPost();
class MetaboxPost
{
private $id = 'plugin_metbox_name';
private $title = 'Meine Metabox';
private $callback = [ $this, 'render_metabox' ];
private $screens = ['post','event'];
private $position = 'side';
private $priority = 'low';
function __construct( )
{
add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] );
}
public function add_metabox()
{
add_meta_box( $this->id, $this->title, $this->callback, $this->screens, $this->position, $this->priority );
}
public function render_metabox()
{
echo "<h1>My Metabox HTML</h1>";
}
}
Version B, statically:
add_action( 'add_meta_boxes', ['MyMetaBox', 'add_metabox'] );
class MyMetaBox
{
private static $ID = 'my_metabox_id';
private static $title = 'My Metabox Title';
private static $render_callback = ['MyMetaBox', 'render_metabox'];
private static $show_on_screens = [ 'post' ];
private static $position = 'side';
private static $priority = 'default';
public static function add_metabox()
{
add_meta_box( self::$ID, self::$title, self::$render_callback, self::$show_on_screens, self::$position, self::$priority );
}
public static function render_metabox()
{
echo "<h1>My Metabox HTML</h1>";
}
}
I feel like Version A creates a more "closes within itself" organization of code, but it kinda annoys me that I have to new construct an Object just to hook into the actions.
