Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Medal checkbox meta

class result_meta_ws {

	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(
			'resultws',
			__( 'Resultaat WorldSkills', 'text_domain' ),
			array( $this, 'render_metabox' ),
			'kandidaat',
			'side',
			'default'
		);

	}

	public function render_metabox( $post ) {

		// Retrieve an existing value from the database.
		$resultws_brons = get_post_meta( $post->ID, 'resultws_brons', true );
		$resultws_zilver = get_post_meta( $post->ID, 'resultws_zilver', true );
		$resultws_goud = get_post_meta( $post->ID, 'resultws_goud', true );
		$resultws_excellence = get_post_meta( $post->ID, 'resultws_excellence', true );
		$resultws_bestofnation = get_post_meta( $post->ID, 'resultws_bestofnation', true );
		$resultws_plaats = get_post_meta( $post->ID, 'resultws_plaats', true );
		$resultws_totaal = get_post_meta( $post->ID, 'resultws_totaal', true );

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

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

		echo '	<tr>';
		echo '		<th><label for="resultws_medal" class="resultws_medal_label">' . __( 'Medaille', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		echo '			<label><input type="checkbox" name="resultws_brons" class="resultws_medal_field" value="' . $resultws_brons . '" ' . checked( $resultws_brons, 'checked', false ) . '> ' . __( 'Brons', 'text_domain' ) . '</label><br>';
		echo '			<label><input type="checkbox" name="resultws_zilver" class="resultws_medal_field" value="' . $resultws_zilver . '" ' . checked( $resultws_zilver, 'checked', false ) . '> ' . __( 'Zilver', 'text_domain' ) . '</label><br>';
		echo '			<label><input type="checkbox" name="resultws_goud" class="resultws_medal_field" value="' . $resultws_goud . '" ' . checked( $resultws_goud, 'checked', false ) . '> ' . __( 'Goud', 'text_domain' ) . '</label><br>';
		echo '			<label><input type="checkbox" name="resultws_excellence" class="resultws_medal_field" value="' . $resultws_excellence . '" ' . checked( $resultws_excellence, 'checked', false ) . '> ' . __( 'Excellence', 'text_domain' ) . '</label><br>';
		echo '			<label><input type="checkbox" name="resultws_bestofnation" class="resultws_medal_field" value="' . $resultws_bestofnation . '" ' . checked( $resultws_bestofnation, 'checked', false ) . '> ' . __( 'Best of Nation', 'text_domain' ) . '</label><br>';
		echo '			<p class="description">' . __( 'Medaille gewonnen?', 'text_domain' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="resultws_plaats" class="resultws_plaats_label">' . __( 'Behaalde plaats', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="number" id="resultws_plaats" name="resultws_plaats" class="resultws_plaats_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $resultws_plaats ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="resultws_totaal" class="resultws_totaal_label">' . __( 'Totaal aantal deelnemers', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="number" id="resultws_totaal" name="resultws_totaal" class="resultws_totaal_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $resultws_totaal ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Sanitize user input.
		$resultws_new_brons = isset( $_POST[ 'resultws_brons' ] ) ? 'checked' : '';
		$resultws_new_zilver = isset( $_POST[ 'resultws_zilver' ] ) ? 'checked' : '';
		$resultws_new_goud = isset( $_POST[ 'resultws_goud' ] ) ? 'checked' : '';
		$resultws_new_excellence = isset( $_POST[ 'resultws_excellence' ] ) ? 'checked' : '';
		$resultws_new_bestofnation = isset( $_POST[ 'resultws_bestofnation' ] ) ? 'checked' : '';
		$resultws_new_plaats = isset( $_POST[ 'resultws_plaats' ] ) ? floatval( $_POST[ 'resultws_plaats' ] ) : '';
		$resultws_new_totaal = isset( $_POST[ 'resultws_totaal' ] ) ? floatval( $_POST[ 'resultws_totaal' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'resultws_brons', $resultws_new_brons );
		update_post_meta( $post_id, 'resultws_zilver', $resultws_new_zilver );
		update_post_meta( $post_id, 'resultws_goud', $resultws_new_goud );
		update_post_meta( $post_id, 'resultws_excellence', $resultws_new_excellence );
		update_post_meta( $post_id, 'resultws_bestofnation', $resultws_new_bestofnation );
		update_post_meta( $post_id, 'resultws_plaats', $resultws_new_plaats );
		update_post_meta( $post_id, 'resultws_totaal', $resultws_new_totaal );

	}

}

new result_meta_ws;