Digger
Digger

Reputation: 738

Multiple check boxes metabox (wordpress)

I'm trying to build a metabox with a bunch of checkbox for my user to choose, and I'm able to make it show on my custom post type edit screen. But the check boxes are not saving... Here's the code for building the checkboxes (which I think is okay):

add_action( 'add_meta_boxes', 'adicionar_metabox' );
function adicionar_metabox()
{
    add_meta_box( 'sobreOProjeto', 'Sobre o projeto', 'projeto_callback', 'Projetos', 'normal', 'default' );
}

function projeto_callback($post) {
    global $post;
    $valores = get_post_custom( $post->ID );
    $urbanidades = isset( $valores['urbanidades'] ) ? esc_attr( $valores['urbanidades'][0] ) : '';
    $comercial = isset( $valores['comercial'] ) ? esc_attr( $valores['comercial'][0] ) : '';
    $habitacao = isset( $valores['habitacao'] ) ? esc_attr( $valores['habitacao'][0] ) : '';
    $institucional = isset( $valores['institucional'] ) ? esc_attr( $valores['institucional'][0] ) : '';
    $efemero = isset( $valores['efemero'] ) ? esc_attr( $valores['efemero'][0] ) : '';
    $objeto = isset( $valores['objeto'] ) ? esc_attr( $valores['objeto'][0] ) : '';

    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );

    ?>
    <p>
        <input type="checkbox" id="urbanidades" name="urbanidades" <?php checked( $urbanidades, 'on' ); ?> />
        <label for="urbanidades">Urbanidades</label>
    </p>
    <p>
        <input type="checkbox" id="comercial" name="comercial" <?php checked( $comercial, 'on' ); ?> />
        <label for="comercial">Comercial</label>
    </p>
    <p>
        <input type="checkbox" id="habitacao" name="habitacao" <?php checked( $habitacao, 'on' ); ?> />
        <label for="habitacao">Habitação</label>
    </p>
    <p>
        <input type="checkbox" id="institucional" name="institucional" <?php checked( $institucional, 'on' ); ?> />
        <label for="institucional">Institucional</label>
    </p>
    <p>
        <input type="checkbox" id="efemero" name="efemero" <?php checked( $efemero, 'on' ); ?> />
        <label for="efemero">Efêmero</label>
    </p>
    <p>
        <input type="checkbox" id="objeto" name="objeto" <?php checked( $objeto, 'on' ); ?> />
        <label for="objeto">Objeto</label>
    </p>
    <?php
}

And this is where the saving should be taking place:

    add_action( 'save_post', 'sobreAObra_salvar' );

        // This is where the saving should be taking place.
        $urbanidades = isset( $_POST['urbanidades'] ) && $_POST['estadoDaObra'] ? 'on' : 'off';
        update_post_meta( $post_id, 'urbanidades', $urbanidades );

        $comercial = isset( $_POST['comercial'] ) && $_POST['estadoDaObra'] ? 'on' : 'off';
        update_post_meta( $post_id, 'comercial', $comercial );

        $habitacao = isset( $_POST['habitacao'] ) && $_POST['estadoDaObra'] ? 'on' : 'off';
        update_post_meta( $post_id, 'habitacao', $habitacao );

        $institucional = isset( $_POST['institucional'] ) && $_POST['estadoDaObra'] ? 'on' : 'off';
        update_post_meta( $post_id, 'institucional', $institucional );

        $efemero = isset( $_POST['efemero'] ) && $_POST['estadoDaObra'] ? 'on' : 'off';
        update_post_meta( $post_id, 'efemero', $efemero );

        $objeto = isset( $_POST['objeto'] ) && $_POST['estadoDaObra'] ? 'on' : 'off';
        update_post_meta( $post_id, 'objeto', $objeto );

    }

Upvotes: 0

Views: 1565

Answers (2)

Digger
Digger

Reputation: 738

Well, probem... solved...?

I followed this tutorial on how to do multiple check boxes. The thing is, it does work as long as I don't use the words I used previously to indicate the options. I don't know if it's because the table field has already been created or something else, but it worked as follows:

<p>
    <input type="checkbox" name="check1" id="check1" value="yes" <?php if ( isset ( $valores['check1'] ) ) checked( $valores['check1'][0], 'yes' ); ?> />
    <label for="check1">Urbanidades</label>
</p>
<p>
    <input type="checkbox" name="check2" id="check2" value="yes" <?php if ( isset ( $valores['check2'] ) ) checked( $valores['check2'][0], 'yes' ); ?> />
    <label for="check2">Comercial</label>
</p>
<p>
    <input type="checkbox" name="check3" id="check3" value="yes" <?php if ( isset ( $valores['check3'] ) ) checked( $valores['check3'][0], 'yes' ); ?> />
    <label for="check3">Habitação</label>
</p>
<p>
    <input type="checkbox" name="check4" id="check4" value="yes" <?php if ( isset ( $valores['check4'] ) ) checked( $valores['check4'][0], 'yes' ); ?> />
    <label for="check4">Institucional</label>
</p>
<p>
    <input type="checkbox" name="check5" id="check5" value="yes" <?php if ( isset ( $valores['check5'] ) ) checked( $valores['check5'][0], 'yes' ); ?> />
    <label for="check5">Efêmero</label>
</p>
<p>
    <input type="checkbox" name="check6" id="check6" value="yes" <?php if ( isset ( $valores['check6'] ) ) checked( $valores['check6'][0], 'yes' ); ?> />
    <label for="check6">Objeto</label>
</p>

Here's how it's being saved. This makes much more sense to me, actually.

    if( isset( $_POST[ 'check1' ] ) ) {
        update_post_meta( $post_id, 'check1', 'yes' );
    } else {
        update_post_meta( $post_id, 'check1', '' );
    }

    if( isset( $_POST[ 'check2' ] ) ) {
        update_post_meta( $post_id, 'check2', 'yes' );
    } else {
        update_post_meta( $post_id, 'check2', '' );
    }

    if( isset( $_POST[ 'check3' ] ) ) {
        update_post_meta( $post_id, 'check3', 'yes' );
    } else {
        update_post_meta( $post_id, 'check3', '' );
    }

    if( isset( $_POST[ 'check4' ] ) ) {
        update_post_meta( $post_id, 'check4', 'yes' );
    } else {
        update_post_meta( $post_id, 'check4', '' );
    }

    if( isset( $_POST[ 'check5' ] ) ) {
        update_post_meta( $post_id, 'check5', 'yes' );
    } else {
        update_post_meta( $post_id, 'check5', '' );
    }

    if( isset( $_POST[ 'check6' ] ) ) {
        update_post_meta( $post_id, 'check6', 'yes' );
    } else {
        update_post_meta( $post_id, 'check6', '' );
    }

Upvotes: 0

Dr.Molle
Dr.Molle

Reputation: 117314

You access the wrong $_POST-keys.

The relevant keys are the name-attributes of the checkboxes, not the id-attributes.

Upvotes: 1

Related Questions