1. Go to this page and download the library: Download inpsyde/metabox-orchestra library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
inpsyde / metabox-orchestra example snippets
MetaboxOrchestra\Bootstrap::bootstrap();
interface PostMetabox extends Metabox {
public function create_info( string $show_or_save, Entity $entity ): BoxInfo;
public function accept( \WP_Post $post, string $save_or_show ): bool;
public function create_view( \WP_Post $post ): BoxView;
public function create_action( \WP_Post $post ): BoxAction;
}
public function create_info( string $show_or_save, Entity $entity ): BoxInfo {
return new BoxInfo( 'My Sample Metabox' );
}
public function __construct( string $title, string $id = '', string $context = '', string $priority = '' )
public function create_info( string $show_or_save, Entity $entity ): BoxInfo {
return new BoxInfo(
__( 'My Sample Metabox', 'my-txt-domain' ),
'sample-metabox',
BoxInfo::CONTEXT_SIDE,
BoxInfo::PRIORITY_HIGH,
);
}
public function create_info( string $show_or_save, Entity $entity ): BoxInfo {
$metabox_name = 'Term';
if ( $entity->is( \WP_Post::class ) ) {
$post_type = get_post_type_object( $entity->post_type );
$metabox_name = $post_type->labels->singular_name;
}
return new BoxInfo( sprintf( 'My %s Metabox', $metabox_name ) );
}
interface BoxView {
public function render( BoxInfo $info ): string;
}
public function create_view( \WP_Post $post ): BoxView {
$view = new MyAwesomeBoxView( $post );
return $view;
}
interface BoxAction {
public function save( AdminNotices $notices ): bool;
}
public function create_action( \WP_Post $post ): BoxAction {
$action = new MyAwesomeBoxAction( $post );
return $this->action;
}
add_action( Boxes::REGISTER_BOXES, function ( Boxes $boxes ) {
$boxes->add_box( new MyAwesomeMetabox() );
} );
namespace MyProject;
use MetaboxOrchestra\Entity;
use MetaboxOrchestra\BoxInfo;
use MetaboxOrchestra\BoxView;
use MetaboxOrchestra\BoxAction;
class SampleMetabox implements MetaboxOrchestra\TermMetabox {
public function create_info( string $show_or_save, Entity $entity ): BoxInfo {
return new BoxInfo( 'My Sample Box' );
}
public function accept_term( \WP_Term $term, string $save_or_show ): bool {
return true;
}
public function view_for_term( \WP_Term $term ): BoxView {
return new SampleView( $term );
}
public function action_for_term( \WP_Term $term ): BoxAction {
return new SampleAction( $term );
}
}
namespace MyProject;
use MetaboxOrchestra\BoxView;
use MetaboxOrchestra\BoxInfo;
class SampleView implements BoxView {
private $term;
public function __construct( \WP_Term $term ) {
$this->term = $term;
}
public function render( BoxInfo $info ): string {
return sprintf(
'<input name="_my_sample_key" type="text" value="%s">',
esc_attr( get_term_meta( $this->term->term_id, '_my_sample_key', TRUE ) ?: '' )
);
}
}