Custom metabox with checkbox not saved no error

Hi, I have the following metabox, but no value is saved, no error at all. Could anybody help me? Thanks a lot, other things on the page are saved correctly.

class cmb_hokuspokus_partneri {

	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(
			'hokuspokus_parneri',
			__( 'Nadpis', 'stavebniforum' ),
			array( $this, 'render_metabox' ),
			'page',
			'normal',
			'default'
		);

	}

	public function render_metabox( $post ) {

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

		// Set default values.

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

		echo '	<tr>';
		echo '		<th><label for="hp_partneri" class="hp_partneri_label">' . __( 'Partneri label', 'stavebniforum' ) . '</label></th>';
		echo '		<td>';
		echo '			<label><input type="checkbox" name="hp_hodnota" class="hp_partneri_field" value="' . $hp_hodnota . '" ' . checked( $hp_hodnota, 'checked', false ) . '> ' . __( 'popisek', 'stavebniforum' ) . '</label>';
		echo '			<label><input type="checkbox" name="hp_dalsihodota" class="hp_partneri_field" value="' . $hp_dalsihodota . '" ' . checked( $hp_dalsihodota, 'checked', true ) . '> ' . __( 'popisekdva', 'stavebniforum' ) . '</label>';
		echo '			<label><input type="checkbox" name="hp_tretihodnota" class="hp_partneri_field" value="' . $hp_tretihodnota . '" ' . checked( $hp_tretihodnota, 'checked', false ) . '> ' . __( 'popisektri', 'stavebniforum' ) . '</label>';
		echo '			<p class="description">' . __( 'Partneri description', 'stavebniforum' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Check if it's not an autosave.
		if ( wp_is_post_autosave( $post_id ) )
			return;

		// Sanitize user input.
		$hp_new_ = isset( $_POST[ 'hp_' ] ) ? 'checked' : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'hp_hodnota', $hp_new_hodnota );
		update_post_meta( $post_id, 'hp_dalsihodota', $hp_new_dalsihodota );
		update_post_meta( $post_id, 'hp_tretihodnota', $hp_new_tretihodnota );

	}

}

new cmb_hokuspokus_partneri;
  • Dimitris

    Hey there Karolina,

    there's a pretty nice WP meta box generator here
    https://jeremyhixon.com/tool/wordpress-meta-box-generator/ :wink:

    I just tried something similar with a triple-checkbox choice in pages and get something like this

    /**
     * Generated by the WordPress Meta Box generator
     * at http://jeremyhixon.com/tool/wordpress-meta-box-generator/
     */
    
    function partneri_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 partneri_add_meta_box() {
    	add_meta_box(
    		'partneri-partneri',
    		__( 'Partneri', 'partneri' ),
    		'partneri_html',
    		'page',
    		'normal',
    		'default'
    	);
    }
    add_action( 'add_meta_boxes', 'partneri_add_meta_box' );
    
    function partneri_html( $post) {
    	wp_nonce_field( '_partneri_nonce', 'partneri_nonce' ); ?>
    
    	<p>
    
    		<input type="checkbox" name="partneri_popisek" id="partneri_popisek" value="popisek" <?php echo ( partneri_get_meta( 'partneri_popisek' ) === 'popisek' ) ? 'checked' : ''; ?>>
    		<label for="partneri_popisek"><?php _e( 'popisek', 'partneri' ); ?></label>	</p>	<p>
    
    		<input type="checkbox" name="partneri_popisekdva" id="partneri_popisekdva" value="popisekdva" <?php echo ( partneri_get_meta( 'partneri_popisekdva' ) === 'popisekdva' ) ? 'checked' : ''; ?>>
    		<label for="partneri_popisekdva"><?php _e( 'popisekdva', 'partneri' ); ?></label>	</p>	<p>
    
    		<input type="checkbox" name="partneri_popisektri" id="partneri_popisektri" value="popisektri" <?php echo ( partneri_get_meta( 'partneri_popisektri' ) === 'popisektri' ) ? 'checked' : ''; ?>>
    		<label for="partneri_popisektri"><?php _e( 'popisektri', 'partneri' ); ?></label>	</p><?php
    }
    
    function partneri_save( $post_id ) {
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
    	if ( ! isset( $_POST['partneri_nonce'] ) || ! wp_verify_nonce( $_POST['partneri_nonce'], '_partneri_nonce' ) ) return;
    	if ( ! current_user_can( 'edit_post', $post_id ) ) return;
    
    	if ( isset( $_POST['partneri_popisek'] ) )
    		update_post_meta( $post_id, 'partneri_popisek', esc_attr( $_POST['partneri_popisek'] ) );
    	else
    		update_post_meta( $post_id, 'partneri_popisek', null );
    	if ( isset( $_POST['partneri_popisekdva'] ) )
    		update_post_meta( $post_id, 'partneri_popisekdva', esc_attr( $_POST['partneri_popisekdva'] ) );
    	else
    		update_post_meta( $post_id, 'partneri_popisekdva', null );
    	if ( isset( $_POST['partneri_popisektri'] ) )
    		update_post_meta( $post_id, 'partneri_popisektri', esc_attr( $_POST['partneri_popisektri'] ) );
    	else
    		update_post_meta( $post_id, 'partneri_popisektri', null );
    }
    add_action( 'save_post', 'partneri_save' );
    
    /*
    	Usage: partneri_get_meta( 'partneri_popisek' )
    	Usage: partneri_get_meta( 'partneri_popisekdva' )
    	Usage: partneri_get_meta( 'partneri_popisektri' )
    */

    As you can notice the implementation is far better and it's actually working.
    Feel free to try this out and post back here if more assistance is required. :slight_smile:

    Warm regards,
    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.