Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

help support brian casel

class Custom_Meta_Box {

	public function __construct() {

		if ( is_admin() ) {
			add_action( 'load-post.php',     array( $this, 'init_metabox' ) );
			add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
		}

	}

	public function init_metabox() {

		add_action( 'add_meta_boxes',        array( $this, 'add_metabox' )         );
		add_action( 'save_post',             array( $this, 'save_metabox' ), 10, 2 );

	}

	public function add_metabox() {

		add_meta_box(
			'mbid',
			__( 'MB', 'text_domain' ),
			array( $this, 'render_metabox' ),
			'post',
			'advanced',
			'default'
		);

	}

	public function render_metabox( $post ) {

		// Retrieve an existing value from the database.
		$custom_fullname = get_post_meta( $post->ID, 'custom_fullname', true );
		$custom_tagline = get_post_meta( $post->ID, 'custom_tagline', true );
		$custom_review = get_post_meta( $post->ID, 'custom_review', true );

		// Set default values.
		if( empty( $custom_fullname ) ) $custom_fullname = '';
		if( empty( $custom_tagline ) ) $custom_tagline = '';
		if( empty( $custom_review ) ) $custom_review = '';

		// Form fields.
		echo '<table class="form-table">';

		echo '	<tr>';
		echo '		<th><label for="custom_fullname" class="custom_fullname_label">' . __( 'Full Name', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="custom_fullname" name="custom_fullname" class="custom_fullname_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr__( $custom_fullname ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="custom_tagline" class="custom_tagline_label">' . __( 'Tagline', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="custom_tagline" name="custom_tagline" class="custom_tagline_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr__( $custom_tagline ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="custom_review" class="custom_review_label">' . __( 'Your Review', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		wp_editor( $custom_review, 'custom_review', array( 'media_buttons' => true ) );
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Sanitize user input.
		$custom_new_fullname = isset( $_POST[ 'custom_fullname' ] ) ? sanitize_text_field( $_POST[ 'custom_fullname' ] ) : '';
		$custom_new_tagline = isset( $_POST[ 'custom_tagline' ] ) ? sanitize_text_field( $_POST[ 'custom_tagline' ] ) : '';
		$custom_new_review = isset( $_POST[ 'custom_review' ] ) ? wp_kses_post( $_POST[ 'custom_review' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'custom_fullname', $custom_new_fullname );
		update_post_meta( $post_id, 'custom_tagline', $custom_new_tagline );
		update_post_meta( $post_id, 'custom_review', $custom_new_review );

	}

}

new Custom_Meta_Box;