Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Formations

class Formations_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(
			'infos-formations',
			__( 'Informations - Formation', 'formations' ),
			array( $this, 'render_metabox' ),
			'formations',
			'advanced',
			'default'
		);

	}

	public function render_metabox( $post ) {

		// Add nonce for security and authentication.
		wp_nonce_field( 'formations_nonce_action', 'formations_nonce' );

		// Retrieve an existing value from the database.
		$formations_formation_duree = get_post_meta( $post->ID, 'formations_formation-duree', true );
		$formations_formation_duree_precision = get_post_meta( $post->ID, 'formations_formation-duree-precision', true );
		$formations_formation_contenu = get_post_meta( $post->ID, 'formations_formation-contenu', true );
		$formations_formation_prealables = get_post_meta( $post->ID, 'formations_formation-prealables', true );
		$formations_formation_materiel = get_post_meta( $post->ID, 'formations_formation-materiel', true );
		$formations_formation_nombre_participants = get_post_meta( $post->ID, 'formations_formation-nombre-participants', true );
		$formations_formation_garantie = get_post_meta( $post->ID, 'formations_formation-garantie', true );

		// Set default values.
		if( empty( $formations_formation_duree ) ) $formations_formation_duree = '';
		if( empty( $formations_formation_duree_precision ) ) $formations_formation_duree_precision = '';
		if( empty( $formations_formation_contenu ) ) $formations_formation_contenu = '';
		if( empty( $formations_formation_prealables ) ) $formations_formation_prealables = '';
		if( empty( $formations_formation_materiel ) ) $formations_formation_materiel = '';
		if( empty( $formations_formation_nombre_participants ) ) $formations_formation_nombre_participants = '';
		if( empty( $formations_formation_garantie ) ) $formations_formation_garantie = array();

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

		echo '	<tr>';
		echo '		<th><label for="formations_formation-duree" class="formations_formation-duree_label">' . __( 'Durée', 'formations' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="number" id="formations_formation_duree" name="formations_formation-duree" class="formations_formation_duree_field" placeholder="' . esc_attr__( '', 'formations' ) . '" value="' . esc_attr( $formations_formation_duree ) . '">';
		echo '			<p class="description">' . __( 'Durée de la formation', 'formations' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="formations_formation-duree-precision" class="formations_formation-duree-precision_label">' . __( 'Durée - précisions', 'formations' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="formations_formation_duree_precision" name="formations_formation-duree-precision" class="formations_formation_duree_precision_field" placeholder="' . esc_attr__( '', 'formations' ) . '" value="' . esc_attr( $formations_formation_duree_precision ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="formations_formation-contenu" class="formations_formation-contenu_label">' . __( 'Contenu (Syllabus)', 'formations' ) . '</label></th>';
		echo '		<td>';
		echo '			<textarea id="formations_formation_contenu" name="formations_formation-contenu" class="formations_formation_contenu_field" placeholder="' . esc_attr__( '', 'formations' ) . '">' . $formations_formation_contenu . '</textarea>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="formations_formation-prealables" class="formations_formation-prealables_label">' . __( 'Préalables', 'formations' ) . '</label></th>';
		echo '		<td>';
		echo '			<textarea id="formations_formation_prealables" name="formations_formation-prealables" class="formations_formation_prealables_field" placeholder="' . esc_attr__( '', 'formations' ) . '">' . $formations_formation_prealables . '</textarea>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="formations_formation-materiel" class="formations_formation-materiel_label">' . __( 'Matériel requis', 'formations' ) . '</label></th>';
		echo '		<td>';
		echo '			<textarea id="formations_formation_materiel" name="formations_formation-materiel" class="formations_formation_materiel_field" placeholder="' . esc_attr__( '', 'formations' ) . '">' . $formations_formation_materiel . '</textarea>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="formations_formation-nombre-participants" class="formations_formation-nombre-participants_label">' . __( 'Nombre maximum de participants', 'formations' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="number" id="formations_formation_nombre_participants" name="formations_formation-nombre-participants" class="formations_formation_nombre_participants_field" placeholder="' . esc_attr__( '', 'formations' ) . '" value="' . esc_attr( $formations_formation_nombre_participants ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="formations_formation-garantie" class="formations_formation-garantie_label">' . __( 'Garantie', 'formations' ) . '</label></th>';
		echo '		<td>';
		echo '			<label><input type="checkbox" name="formations_formation-garantie[]" class="formations_formation_garantie_field" value="' . esc_attr( '' ) . '" ' . ( in_array( '', $formations_formation-garantie )? 'checked="checked"' : '' ) . '> ' . __( '', 'formations' ) . '</label><br>';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['formations_nonce'] ) ? $_POST['formations_nonce'] : '';
		$nonce_action = 'formations_nonce_action';

		// Check if a nonce is set.
		if ( ! isset( $nonce_name ) )
			return;

		// Check if a nonce is valid.
		if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
			return;

		// Check if the user has permissions to save data.
		if ( ! current_user_can( 'edit_post', $post_id ) )
			return;

		// Sanitize user input.
		$formations_new_formation_duree = isset( $_POST[ 'formations_formation-duree' ] ) ? floatval( $_POST[ 'formations_formation-duree' ] ) : '';
		$formations_new_formation_duree_precision = isset( $_POST[ 'formations_formation-duree-precision' ] ) ? sanitize_text_field( $_POST[ 'formations_formation-duree-precision' ] ) : '';
		$formations_new_formation_contenu = isset( $_POST[ 'formations_formation-contenu' ] ) ? sanitize_text_field( $_POST[ 'formations_formation-contenu' ] ) : '';
		$formations_new_formation_prealables = isset( $_POST[ 'formations_formation-prealables' ] ) ? sanitize_text_field( $_POST[ 'formations_formation-prealables' ] ) : '';
		$formations_new_formation_materiel = isset( $_POST[ 'formations_formation-materiel' ] ) ? sanitize_text_field( $_POST[ 'formations_formation-materiel' ] ) : '';
		$formations_new_formation_nombre_participants = isset( $_POST[ 'formations_formation-nombre-participants' ] ) ? floatval( $_POST[ 'formations_formation-nombre-participants' ] ) : '';
		$formations_new_formation_garantie = isset( $_POST[ 'formations_formation-garantie' ] ) ? array_intersect( (array) $_POST[ 'formations_formation-garantie' ], array( '' ) )  : array();

		// Update the meta field in the database.
		update_post_meta( $post_id, 'formations_formation-duree', $formations_new_formation_duree );
		update_post_meta( $post_id, 'formations_formation-duree-precision', $formations_new_formation_duree_precision );
		update_post_meta( $post_id, 'formations_formation-contenu', $formations_new_formation_contenu );
		update_post_meta( $post_id, 'formations_formation-prealables', $formations_new_formation_prealables );
		update_post_meta( $post_id, 'formations_formation-materiel', $formations_new_formation_materiel );
		update_post_meta( $post_id, 'formations_formation-nombre-participants', $formations_new_formation_nombre_participants );
		update_post_meta( $post_id, 'formations_formation-garantie', $formations_new_formation_garantie );

	}

}

new Formations_Meta_Box;