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
}
?>
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: