Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Tax Meta Box

Fachgebiet-Seite

class Sgr_Tax_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(
			'tax_meta',
			__( 'Fachbereich', 'sgr' ),
			array( $this, 'render_tax_meta' ),
			'page',
			'normal',
			'default'
		);

	}

	public function render_tax_meta( $post ) {

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

		// Set default values.
		if( empty( $sgr_tax ) ) $sgr_tax = '';

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

		echo '	<tr>';
		echo '		<th><label for="sgr_tax" class="sgr_tax_label">' . __( 'Fachbereich', 'sgr' ) . '</label></th>';
		echo '		<td>';
		wp_dropdown_categories( array( 'id' => 'sgr_tax', 'name' => 'sgr_tax', 'class' => 'sgr_tax_field', 'selected' => $sgr_tax ) );
		echo '			<p class="description">' . __( 'Wähle aus dem Fachbereich aus', 'sgr' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Sanitize user input.
		$sgr_new_tax = isset( $_POST[ 'sgr_tax' ] ) ? sanitize_text_field( $_POST[ 'sgr_tax' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'sgr_tax', $sgr_new_tax );

	}

}

new Sgr_Tax_Meta_Box;