The WordPress User Profile page comes with basic fields. Name, email, password, and role are a few of these. But, what if you need to know your users interests, or phone number, or their gender?
You would add some radio buttons or checkbox fields to gather this information.
Adding Custom User Profile fields; different input field types
Justin Tadlock’s most helpful post post, “Adding and using custom user profile fields” http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields. has instructions for adding a text field to the WordPress User Profile Editor.
Different field types for the WordPress User Profile Page
Once you have read his post and added the function to your functions.php file in your child theme, you can add the following code if you need one of these different input field types.
Radio Button Field
<tr> <th><label for="gender">Gender</label></th> <td> <input type="radio" name="gender" value="Male" <?php if('Male'==esc_attr(get_the_author_meta('gender',$user->ID ))) echo 'checked="checked"'; ?> class="radio" /> Male<br /> <input type="radio" name="gender" value="Female" <?php if('Female'==esc_attr(get_the_author_meta('gender',$user->ID ))) echo 'checked="checked"'; ?> class="radio" /> Female<br /> <input type="radio" name="gender" value="Prefer Not to Answer" <?php if ('Prefer Not to Answer'==esc_attr(get_the_author_meta('gender',$user->ID ))) echo 'checked="checked"'; ?> class="radio" /> Prefer Not to Answer </td> </tr>
Checkbox field type: (multiple choices)
<tr> <th><label for="dnp_cklist">DNP Options</label></th> <td><input type="checkbox" name="Do not publish my phone in the printed directory" <?php if('on'==esc_attr(get_the_author_meta('dnp_cklist',$user->ID ))) echo 'checked="checked"'; ?>/> Do not publish my phone in the printed directory<br /> <input type="checkbox" name="Do not publish my age in the printed directory" <?php if('on'==esc_attr(get_the_author_meta('dnp_cklist',$user->ID ))) echo 'checked="checked"'; ?>/> Do not publish my age in the printed directory<br /> <input type="checkbox" name="Do not publish my phone in the members area" <?php if('on'==esc_attr(get_the_author_meta('dnp_cklist',$user->ID ))) echo 'checked="checked"'; ?>/> Do not publish my phone in the members area<br /> <input type="checkbox" name="Do not publish my age in the members area" <?php if('on'==esc_attr(get_the_author_meta('dnp_cklist',$user->ID ))) echo 'checked="checked"'; ?>/> Do not publish my age in the members area<br /> <input type="checkbox" name="Do not list me at all in the members area" <?php if('on'==esc_attr(get_the_author_meta('dnp_cklist',$user->ID ))) echo 'checked="checked"'; ?>/> Do not list me at all in the members area <br /> <input type="checkbox" name="List everything in both directories" <?php if('on'==esc_attr(get_the_author_meta('dnp_cklist',$user->ID ))) echo 'checked="checked"'; ?>/> List everything in both directories<br /> </td> </tr>
Static Fields
I also wanted to make some static fields, e.g. Join Date, so the user could not change the automatically generated data:
<tr> <th><label for="joindate">Join Date</label></th> <td> <?php echo esc_attr( get_the_author_meta( 'joindate', $user->ID ) ); ?><br /> </tr
Saving Entered Data
Don’t forget to put the function to save the data in the custom field:
Just because we’re displaying these extra fields, doesn’t mean they’ll be saved when the user profile is updated. So, we need one more function to handle this. Drop this PHP code in your theme’s functions.php file.
Justin Tadlock’s code
//Saving the custom user fields 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 ( !current_user_can( 'edit_user', $user_id ) )return false;
/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */ if(isset($_POST['gender'])){ update_user_meta( $user_id, 'gender', $_POST['gender'] ); } if(isset($_POST['joindate'])){ update_user_meta( $user_id, 'joindate', $_POST['joindate'] ); }
This part of the php says “check to see if there is something in the field”
if(isset($_POST['joindate']))
Alternately: John King has a Utube video on this subject with mostly the same information. Additionally, he includes how to add your new fields to a Gravity Forms Registration form.