NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Easy
Intermediate
Hard

---- Last modified : Sep 05 2022

Estimated reading time : 4 minutes, 33 seconds - 211 words

This tutorial will help you learn how to create a metabox allowing users to collect data from a Post or custom Post Type. In the example below, we will use Themespress.ca as an example.

We want to create a Metabox with the following structure:
– Easy
– Intermediate
– Hard

This metabox will let you measure the post or post type for users. There are multiple ways to create a metabox. We recommend:

Build and Display a WordPress Metabox

function statut_diffucult_get_meta( $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 statut_diffucult_add_meta_box() {
	add_meta_box(
		'statut_diffucult-statut-diffucult',
		__( 'Statut Difficulté', 'statut_diffucult' ),
		'statut_diffucult_html',
                'post',
		'side',
		'high'
	);
}
add_action( 'add_meta_boxes', 'statut_diffucult_add_meta_box' );

In this function statut_hard_add_meta_box, we can figure out how to build our structure. We also use it for WordPress posts. (‘post’) == // Changer pour votre Post Type.

The side of our function represents the positioning in the Post Type.

In this case, the side is the lateral bar of WordPress. The High defines its priority; in this case we want it higher because we want it at the top. The metabox is for the posts and will be positioned in the top right section of the post you’re currently editing.

Now we want our metabox to include three checkbox; one for each choice: Easy, Intermediate, Hard. It’s also done so we can save the checkbox (or checkboxes) selected.

function statut_diffucult_html( $post) {
	wp_nonce_field( '_statut_diffucult_nonce', 'statut_diffucult_nonce' ); ?>
	<p>
        <input type="checkbox" name="statut_diffucult_facile" id="statut_diffucult_facile" value="facile" <?php echo ( statut_diffucult_get_meta( 'statut_diffucult_facile' ) === 'facile' ) ? 'checked' : ''; ?>>
		<label for="statut_diffucult_facile"><?php _e( 'Facile', 'crio-child' ); ?></label>	
		
	</p>	
	<p>
        <input type="checkbox" name="statut_diffucult_intermdiaire" id="statut_diffucult_intermdiaire" value="intermdiaire" <?php echo ( statut_diffucult_get_meta( 'statut_diffucult_intermdiaire' ) === 'intermdiaire' ) ? 'checked' : ''; ?>>
		<label for="statut_diffucult_intermdiaire"><?php _e( 'Intermédiaire', 'crio-child' ); ?></label>	
	</p>	
	<p>
	    <input type="checkbox" name="statut_diffucult_difficile" id="statut_diffucult_difficile" value="difficile" <?php echo ( statut_diffucult_get_meta( 'statut_diffucult_difficile' ) === 'difficile' ) ? 'checked' : ''; ?>>
		<label for="statut_diffucult_difficile"><?php _e( 'Difficile', 'crio-child' ); ?></label>	
	</p>	
	<p>
	    <input type="checkbox" name="statut_diffucult_expert" id="statut_diffucult_expert" value="expert" <?php echo ( statut_diffucult_get_meta( 'statut_diffucult_expert' ) === 'expert' ) ? 'checked' : ''; ?>>
		<label for="statut_diffucult_expert"><?php _e( 'Expert', 'crio-child' ); ?></label>	
	</p>
<?php
}

function statut_diffucult_save( $post_id ) {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	if ( ! isset( $_POST['statut_diffucult_nonce'] ) || ! wp_verify_nonce( $_POST['statut_diffucult_nonce'], '_statut_diffucult_nonce' ) ) return;
	if ( ! current_user_can( 'edit_post', $post_id ) ) return;

	if ( isset( $_POST['statut_diffucult_facile'] ) )
		update_post_meta( $post_id, 'statut_diffucult_facile', esc_attr( $_POST['statut_diffucult_facile'] ) );
	else
		update_post_meta( $post_id, 'statut_diffucult_facile', null );
	
	
	if ( isset( $_POST['statut_diffucult_intermdiaire'] ) )
		update_post_meta( $post_id, 'statut_diffucult_intermdiaire', esc_attr( $_POST['statut_diffucult_intermdiaire'] ) );
	else
		update_post_meta( $post_id, 'statut_diffucult_intermdiaire', null );
	
	
	if ( isset( $_POST['statut_diffucult_difficile'] ) )
		update_post_meta( $post_id, 'statut_diffucult_difficile', esc_attr( $_POST['statut_diffucult_difficile'] ) );
	else
		update_post_meta( $post_id, 'statut_diffucult_difficile', null );
	
	if ( isset( $_POST['statut_diffucult_expert'] ) )
		update_post_meta( $post_id, 'statut_diffucult_expert', esc_attr( $_POST['statut_diffucult_expert'] ) );
	else
		update_post_meta( $post_id, 'statut_diffucult_expert', null );
	
	
}
add_action( 'save_post', 'statut_diffucult_save' );

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>