I have this code to add a custom field to user profile. It works fine but it is only useful for 'text' input type.
I need to add a 'radio' input type. I need help with the code.
/***********************************************************/
/********** Custom User field ************/
/***********************************************************/
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) {
?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="equivalence_surcharge"><?php _e("Equivalence Surcharge"); ?></label></th>
<td>
<input type="text" name="equivalence_surcharge" id="equivalence_surcharge" value="<?php echo esc_attr( get_the_author_meta( 'equivalence_surcharge', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Equivalence Surcharge"); ?></span>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $user_id ) ) {
return;
}
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'equivalence_surcharge', $_POST['equivalence_surcharge'] );
}
To add radio buttons input field to user profile use the following:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
You will get something like:
Display the radio buttons in a row (horizontaly):
You can display the radio buttons in a row by changing:
with:
and you will get something like: