-1

I am trying to create a checkbox that saves multiple values like those below, i am working on a costum field for wordpress users. The problem is that I can only save the last checked box if i check any box above that one, it won't work. I want to be able to save as many values as the user checks ,one,two or all. Then I want to be able to read them one by one. I have no idea how to change this. But the saving part on the user_meta is working correctly because it saves the last value.

    <p> <p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
        <label for="area_profissao"><?php _e( 'Área de Intervenção Profissional'); ?></label><br/>
        <input type="checkbox" class="checkbox" name="area_profissao" id="area_profissao1" value="CAPDA" <?php if (get_the_author_meta( 'area_profissao', $user->ID) == 'CAPDA' ) { ?>checked="checked"<?php }?> />Crianças e adolescentes com perturbações do desenvolvimento e aprendizagem <br />
        <input type="checkbox" class="checkbox" name="area_profissao" id="area_profissao2" value="CMPIP" <?php if (get_the_author_meta( 'area_profissao', $user->ID) == 'CMPIP' ) { ?>checked="checked"<?php }?> />Crianças em meio pré-escolar e/ou Intervenção Precoce<br />
        <input type="checkbox" class="checkbox" name="area_profissao" id="area_profissao3" value="CACP" <?php if (get_the_author_meta( 'area_profissao', $user->ID) == 'CACP' ) { ?>checked="checked"<?php }?> />Crianças e adolescentes em contexto pedopsiquiátrico<br />
        <input type="checkbox" class="checkbox" name="area_profissao" id="area_profissao4" value="MA" <?php if (get_the_author_meta( 'area_profissao', $user->ID) == 'MA' ) { ?>checked="checked"<?php }?> />Meio Aquático<br />
        <input type="checkbox" class="checkbox" name="area_profissao" id="area_profissao5" value="SMAI" <?php if (get_the_author_meta( 'area_profissao', $user->ID) == 'SMAI' ) { ?>checked="checked"<?php }?> />Saúde mental do adulto e do idoso<br />
    </p>

function yoursite_save_extra_user_profile_fields( $user_id ) {
$saved = false;

if ( is_admin() && current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'nsocio', $_POST['nsocio'] );
    $saved = true;
}


if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'nif', $_POST['nif'] );
    $saved = true;
}

if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'telemovel', $_POST['telemovel'] );
    $saved = true;
}

if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'hb_litera', $_POST['hb_litera'] );
    $saved = true;
}

if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'ano_il', $_POST['ano_il'] );
    $saved = true;
}

if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'ano_fl', $_POST['ano_fl'] );
    $saved = true;
}

if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'nome_uni', $_POST['nome_uni'] );
    $saved = true;
}

if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'profissao', $_POST['profissao'] );
    $saved = true;
}

if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'area_profissao', $_POST['area_profissao'] );
    $saved = true;
}  

if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'distrito_profissao', $_POST['distrito_profissao'] );
    $saved = true;
}

if ( current_user_can( 'edit_user', $user_id ) ) {
    update_usermeta( $user_id, 'list_prof', $_POST['list_prof'] );
    $saved = true;
}  

return true;

}

1 Answers1

0

You simply need to add [] to the end of the checkbox names to make them behave as an array on the PHP side, like so:

<input type="checkbox" class="checkbox" name="area_profissao[]" ... > 

Be warned however that this will turn the content of your meta fields into arrays so your get_the_author_meta( 'area_profissao', $user->ID) == 'CAPDA' checks will need to be rewritten to work with this setup, same with the saving mechanism.

ppajer
  • 3,045
  • 1
  • 15
  • 22