Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to use checkboxes on registration form, but values are not storing in the profile page.

here is the code on functions.php

<?php
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>

<h3>Settori di interesse</h3>
<table class="form-table">
    <tr>
        <td>
            <input type="checkbox" name="user_silos" id="user_silos" value="true" <?php if (esc_attr( get_the_author_meta( "user_silos", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_silos">Silos</label>
        </td>
        <td>
            <input type="checkbox" name="user_biodigestori" id="user_biodigestori" value="true" <?php if (esc_attr( get_the_author_meta( "user_biodigestori", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_biodigestori">Biodigestori</label>
        </td>
        <td>
            <input type="checkbox" name="user_unitratt" id="user_unitratt" value="true" <?php if (esc_attr( get_the_author_meta( "user_unitratt", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_unitratt">Unità di trattamento</label>
        </td>
        <td>
            <input type="checkbox" name="user_imt" id="user_imt" value="true" <?php if (esc_attr( get_the_author_meta( "user_imt", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_imt">Impianti di media tensione</label>
        </td>
        <td>
            <input type="checkbox" name="user_cabelet" id="user_cabelet" value="true" <?php if (esc_attr( get_the_author_meta( "user_cabelet", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_cabelet">Cabine elettriche</label>
        </td>
        <td>
            <input type="checkbox" name="user_zoot" id="user_zoot" value="true" <?php if (esc_attr( get_the_author_meta( "user_zoot", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_zoot">Zootecnica</label>
        </td>
    </tr>
</table>
<?php }?>


<?php
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {
    if (!isset($_POST['user_silos'])) $_POST['user_silos'] = "false"; 
    update_user_meta( $user_id, 'user_silos', $_POST['user_silos'] );
    if (!isset($_POST['user_biodigestori'])) $_POST['user_biodigestori'] = "false"; 
    update_user_meta( $user_id, 'user_biodigestori', $_POST['user_biodigestori'] );
    if (!isset($_POST['user_unitratt'])) $_POST['user_unitratt'] = "false"; 
    update_user_meta( $user_id, 'user_unitratt', $_POST['user_unitratt'] );
    if (!isset($_POST['user_imt'])) $_POST['user_imt'] = "false"; 
    update_user_meta( $user_id, 'user_imt', $_POST['user_imt'] );
    if (!isset($_POST['user_cabelet'])) $_POST['user_cabelet'] = "false"; 
    update_user_meta( $user_id, 'user_cabelet', $_POST['user_cabelet'] );
    if (!isset($_POST['user_zoot'])) $_POST['user_zoot'] = "false"; 
    update_user_meta( $user_id, 'user_zoot', $_POST['user_zoot'] );     
}
?>

on the registration page I have this code

 <div class="reg-row">
    <span class="ckb">
        <input type="checkbox" name="user_silos" id="user_silos" value="true" <?php if (esc_attr( get_the_author_meta( "user_silos", $user->ID )) == "true") echo "checked"; ?> />
        <label for="user_silos">Silos</label>
    </span>
    <span class="ckb">
        <input type="checkbox" name="user_biodigestori" id="user_biodigestori" value="true" <?php if (esc_attr( get_the_author_meta( "user_biodigestori", $user->ID )) == "true") echo "checked"; ?> />
        <label for="user_biodigestori">Biodigestori</label>
    </span>
    <span class="ckb">
        <input type="checkbox" name="user_unitratt" id="user_unitratt" value="true" <?php if (esc_attr( get_the_author_meta( "user_unitratt", $user->ID )) == "true") echo "checked"; ?> />
        <label for="user_unitratt">Unità di trattamento</label>
    </span>
 </div> 

 <div class="reg-row">
    <span class="ckb">
        <input type="checkbox" name="user_imt" id="user_imt" value="true" <?php if (esc_attr( get_the_author_meta( "user_imt", $user->ID )) == "true") echo "checked"; ?> />
        <label for="user_imt">Impianti di media tensione</label>
    </span>
    <span class="ckb">
        <input type="checkbox" name="user_cabelet" id="user_cabelet" value="true" <?php if (esc_attr( get_the_author_meta( "user_cabelet", $user->ID )) == "true") echo "checked"; ?> />
        <label for="user_cabelet">Cabine elettriche</label>
    </span>
    <span class="ckb">
        <input type="checkbox" name="user_zoot" id="user_zoot" value="true" <?php if (esc_attr( get_the_author_meta( "user_zoot", $user->ID )) == "true") echo "checked"; ?> />
        <label for="user_zoot">Zootecnia</label>
    </span>
 </div> 

Does anyone know where I'm wrong? thanks for the help


EDIT

With multiple checkboxes it doesn't work...

If during the registration there aren't selections, on the profile page the checkboxes are all selected.

<?php

add_action( 'register_form', 'signup_fields_ckb' );
add_action( 'user_register', 'handle_signup_ckb', 10, 2 );
add_action( 'show_user_profile', 'user_field_ckb' );
add_action( 'edit_user_profile', 'user_field_ckb' );

function signup_fields_ckb() {
?>

    <p class="txt-green">Settore di interesse</p>

     <div class="reg-row">
        <span class="ckb">
            <input type="checkbox" name="user_silos" id="user_silos" value="true" <?php if (esc_attr( get_the_author_meta( "user_silos", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_silos">Silos</label>
        </span>
        <span class="ckb">
            <input type="checkbox" name="user_biodigestori" id="user_biodigestori" value="true" <?php if (esc_attr( get_the_author_meta( "user_biodigestori", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_biodigestori">Biodigestori</label>
        </span>
        <span class="ckb">
            <input type="checkbox" name="user_unitratt" id="user_unitratt" value="true" <?php if (esc_attr( get_the_author_meta( "user_unitratt", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_unitratt">Unità di trattamento</label>
        </span>
     </div> 

     <div class="reg-row">
        <span class="ckb ckb-imt">
            <input type="checkbox" name="user_imt" id="user_imt" value="true" <?php if (esc_attr( get_the_author_meta( "user_imt", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_imt">Impianti di media tensione</label>
        </span>
        <span class="ckb">
            <input type="checkbox" name="user_cabelet" id="user_cabelet" value="true" <?php if (esc_attr( get_the_author_meta( "user_cabelet", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_cabelet">Cabine elettriche</label>
        </span>
        <span class="ckb">
            <input type="checkbox" name="user_cooger" id="user_cooger" value="true" <?php if (esc_attr( get_the_author_meta( "user_cooger", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_cooger">Coogeneratori</label>
        </span>
     </div>  

     <div class="reg-row">
        <span class="ckb">
            <input type="checkbox" name="user_zoot" id="user_zoot" value="true" <?php if (esc_attr( get_the_author_meta( "user_zoot", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_zoot">Zootecnica</label>
        </span>
        <span class="ckb">
            <input type="checkbox" name="user_attagri" id="user_attagri" value="true" <?php if (esc_attr( get_the_author_meta( "user_attagri", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_attagri">Attrezzature Agricole</label>
        </span>
        <span class="ckb">
            <input type="checkbox" name="user_agriwat" id="user_agriwat" value="true" <?php if (esc_attr( get_the_author_meta( "user_agriwat", $user->ID )) == "true") echo "checked"; ?> />
            <label for="user_agriwat">Agri Water</label>
        </span>
     </div>

<?php
}

function handle_signup_ckb( $user_id, $data = null ) 
{
    $user_silos = $_REQUEST['user_silos'];
    $user_biodigestori = $_REQUEST['user_biodigestori'];
    $user_unitratt = $_REQUEST['user_unitratt'];
    $user_imt = $_REQUEST['user_imt'];
    $user_cabelet = $_REQUEST['user_cabelet'];
    $user_cooger = $_REQUEST['user_cooger'];
    $user_zoot = $_REQUEST['user_zoot'];
    $user_attagri = $_REQUEST['user_attagri'];
    $user_silos = $_REQUEST['user_agriwat'];

    if ( isset( $user_silos ) ) { add_user_meta( $user_id, 'user_silos', $user_silos );}
    if ( isset( $user_biodigestori ) ) { add_user_meta( $user_id, 'user_biodigestori', $user_biodigestori );}
    if ( isset( $user_unitratt ) ) { add_user_meta( $user_id, 'user_unitratt', $user_unitratt );}
    if ( isset( $user_imt ) ) { add_user_meta( $user_id, 'user_imt', $user_imt );}
    if ( isset( $user_cabelet ) ) { add_user_meta( $user_id, 'user_cabelet', $user_cabelet );}
    if ( isset( $user_cooger ) ) { add_user_meta( $user_id, 'user_cooger', $user_cooger );}
    if ( isset( $user_zoot ) ) { add_user_meta( $user_id, 'user_zoot', $user_zoot );}
    if ( isset( $user_attagri ) ) { add_user_meta( $user_id, 'user_attagri', $user_attagri );}
    if ( isset( $user_agriwat ) ) { add_user_meta( $user_id, 'user_agriwat', $user_agriwat );}
}



function user_field_ckb( $user ) 
{
    $user_silos = get_the_author_meta( 'user_silos', $user->ID );
    $user_biodigestori = get_the_author_meta( 'user_biodigestori', $user->ID );
    $user_unitratt = get_the_author_meta( 'user_unitratt', $user->ID );
    $user_imt = get_the_author_meta( 'user_imt', $user->ID );
    $user_cabelet = get_the_author_meta( 'user_cabelet', $user->ID );
    $user_cooger = get_the_author_meta( 'user_cooger', $user->ID );
    $user_zoot = get_the_author_meta( 'user_zoot', $user->ID );
    $user_attagri = get_the_author_meta( 'user_attagri', $user->ID );
    $user_silos = get_the_author_meta( 'user_agriwat', $user->ID );
?>

    <table class="form-table">
      <tr>
        <th>
          <td><span class="description"><?php _e('Silos'); ?></span><br>
          <label><?php printf('<input type="checkbox" name="user_silos" id="user_silos" %1$s />', checked( $user_silos, '', false ));?></label>
          </td>
          <td><span class="description"><?php _e('Biodigestori'); ?></span><br>
          <label><?php printf('<input type="checkbox" name="user_biodigestori" id="user_biodigestori" %1$s />', checked( $user_biodigestori, '', false ));?></label>
          </td>
          <td><span class="description"><?php _e('Unità di trattamento'); ?></span><br>
          <label><?php printf('<input type="checkbox" name="user_unitratt" id="user_unitratt" %1$s />', checked( $user_unitratt, '', false ));?></label>
          </td>
          <td><span class="description"><?php _e('Impianti di media tensione'); ?></span><br>
          <label><?php printf('<input type="checkbox" name="user_imt" id="user_imt" %1$s />', checked( $user_imt, '', false ));?></label>
          </td>
          <td><span class="description"><?php _e('Cabine elettriche'); ?></span><br>
          <label><?php printf('<input type="checkbox" name="user_cabelet" id="user_cabelet" %1$s />', checked( $user_cabelet, '', false ));?></label>
          </td>
          <td><span class="description"><?php _e('Coogeneratori'); ?></span><br>
          <label><?php printf('<input type="checkbox" name="user_cooger" id="user_cooger" %1$s />', checked( $user_cooger, '', false ));?></label>
          </td>
          <td><span class="description"><?php _e('Zootecnica'); ?></span><br>
          <label><?php printf('<input type="checkbox" name="user_zoot" id="user_zoot" %1$s />', checked( $user_zoot, '', false ));?></label>
          </td>
          <td><span class="description"><?php _e('Attrezzature agricole'); ?></span><br>
          <label><?php printf('<input type="checkbox" name="user_attagri" id="user_attagri" %1$s />', checked( $user_attagri, '', false ));?></label>
          </td>
          <td><span class="description"><?php _e('Agriwater'); ?></span><br>
          <label><?php printf('<input type="checkbox" name="user_agriwat" id="user_agriwat" %1$s />', checked( $user_agriwat, '', false ));?></label>
          </td>
      </tr>
    </table>
<?php 
}
?>
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Short of creating a custom registration page, there are not much hooks that can be used to add custom fields to the default form. I tried once (with register_form hook) and only default fields (description, website, etc) were successfully recognized and saved.

Found this Q&A that has a workaround: Adding extra info via GET while registeration in wordpress

Here's a proof of concept:

// REGISTRATION
add_action( 'register_form', 'signup_fields_wpse_87261' );
add_action( 'user_register', 'handle_signup_wpse_87261', 10, 2 );

// PROFILE
add_action( 'show_user_profile', 'user_field_wpse_87261' );
add_action( 'personal_options_update', 'save_profile_fields_87261' );

// USER EDIT
add_action( 'edit_user_profile', 'user_field_wpse_87261' );
add_action( 'edit_user_profile_update', 'save_profile_fields_87261' );

function signup_fields_wpse_87261() {
?>
    <label>
        <input type="checkbox" name="custom_feature_a" id="custom_feature_a" /> 
        Enable feature A?
    </label>
    <br />
    <label>
        <input type="checkbox" name="custom_feature_b" id="custom_feature_b" /> 
        Enable feature B?
    </label>
    <hr />
<?php
}

function handle_signup_wpse_87261( $user_id, $data = null ) 
{
    $feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
    $feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
    if ( $feat_a ) 
    {
        add_user_meta( $user_id, 'custom_feature_a', $feat_a );
    }
    if ( $feat_b ) 
    {
        add_user_meta( $user_id, 'custom_feature_b', $feat_b );
    }
}

function user_field_wpse_87261( $user ) 
{
    $feat_a = get_user_meta( $user->ID, 'custom_feature_a', true );
    $feat_b = get_user_meta( $user->ID, 'custom_feature_b', true );
?>
    <h3><?php _e('Custom Fields'); ?></h3>
    <table class="form-table">
        <tr>
            <td>
                <label><?php 
                    printf(
                        '<input type="checkbox" name="custom_feature_a" id="custom_feature_a" %1$s />',
                        checked( $feat_a, 'on', false )
                    );
                    ?>
                    <span class="description"><?php _e('Custom Feature A?'); ?></span>
                    </label>
            </td>
        </tr>
        <tr>
            <td>
                <label><?php 
                    printf(
                        '<input type="checkbox" name="custom_feature_b" id="custom_feature_b" %1$s />',
                        checked( $feat_b, 'on', false )
                    );
                    ?>
                    <span class="description"><?php _e('Custom Feature B?'); ?></span>
                    </label>
            </td>
        </tr>
    </table>
<?php 
}

function save_profile_fields_87261( $user_id ) 
{
    $feat_a = isset( $_POST['custom_feature_a'] ) ? $_POST['custom_feature_a'] : false;
    $feat_b = isset( $_POST['custom_feature_b'] ) ? $_POST['custom_feature_b'] : false;
    update_usermeta( $user_id, 'custom_feature_a', $feat_a );
    update_usermeta( $user_id, 'custom_feature_b', $feat_b );
}
share|improve this answer
    
Thank you @brasofilo ..it works! –  Epaxido Feb 26 '13 at 13:36
    
doesn't work with multiple chekboxes –  Epaxido Feb 26 '13 at 14:28
    
I've corrected the code but it still doesn't work.. now are all unselected. (ps. Even changing the checkbox in the profile values ​​are not stored) –  Epaxido Feb 26 '13 at 15:55
    
@Epaxido, code revised and updated, working with two fields now. –  brasofilo Feb 26 '13 at 16:47
    
Thanks @brasofilo ..works great! I think this will be useful for many people. –  Epaxido Feb 27 '13 at 10:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.