Metabox für Beiträge in Gutenberg Seitenleiste aufnehmen

Der Code hier macht, dass in der Seitenleiste von Gutenberg eine Metabox bei Posts reingestellt werden kann. Dieser Code kann auch genutzt werden für alle anderen Metaboxen.

<?php
 
/**
 * Generated by the WordPress Meta Box generator
 * at http://jeremyhixon.com/tool/wordpress-meta-box-generator/
 */
 
function get_meta_darstellungs_optionen( $value ) {
	global $post;
 
	$field = get_post_meta( $post->ID, $value, true );
	if ( ! empty( $field ) ) {
		return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
	} else {
		return false;
	}
}
 
function add_meta_box_darstellungs_optionen() {
 
	$id       = 'brandgrad-darstellungs-optionen';
	$title    = 'Darstellungs Optionen';
	$callback = 'mb_darstellungs_optionen_html';
	$screen   = 'post';
	$context  = 'side';
	$priority = 'high';
	add_meta_box( $id, $title, $callback, $screen,$context, $priority );
}
add_action( 'add_meta_boxes', 'add_meta_box_darstellungs_optionen' );
 
function mb_darstellungs_optionen_html( $post) {
	wp_nonce_field( '_darstellungs_optionen_nonce', 'darstellungs_optionen_nonce' );
	?>
 
	<p>
		<input type="checkbox" name="display_as_wide" id="display_as_wide" value="important" <?php echo ( get_meta_darstellungs_optionen( 'display_as_wide' ) === 'important' ) ? 'checked' : ''; ?>>
		<label for="display_as_wide">Darstellung als breite Box</label>
	</p>
	<?php
}
 
function mb_darstellungs_optioen_save( $post_id ) {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	if ( ! isset( $_POST['darstellungs_optionen_nonce'] ) || ! wp_verify_nonce( $_POST['darstellungs_optionen_nonce'], '_darstellungs_optionen_nonce' ) ) return;
	if ( ! current_user_can( 'edit_post', $post_id ) ) return;
 
	if ( isset( $_POST['display_as_wide'] ) )
		update_post_meta( $post_id, 'display_as_wide', esc_attr( $_POST['display_as_wide'] ) );
	else
		update_post_meta( $post_id, 'display_as_wide', null );
}
add_action( 'save_post', 'mb_darstellungs_optioen_save' );
 
/*
	Usage: get_meta_darstellungs_optionen( 'display_as_wide' )
*/

Page Tools