How to add a field on the WordPress Users profile

How to add a field on the WordPress Users profile

If you want to add an extra field on WordPress User profile, then we can do it using plugins or adding some code in “functions.php” . In this tutorial I using the second method. Each and every WordPress theme has a file called functions.php , we have to just open this file and paste the code given below

Code given below will create some extra field like contact, gender


//extra fields
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){
//$user_id=1;//sanitize_text_field($_GET['user_id']);
?> <h3>Contact No.</h3>
<table class="form-table">
<tr>
<th><label for="twitter">Contact</label></th>
<td>
<input type="text" name="contact" id="contact" value="<?php echo esc_attr( get_the_author_meta( 'contact', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your Contact No.</span>
</td>
</tr>
<tr>
<th><label for="twitter">Gender</label></th>
<td>
<select name="gender" id="gender" class="regular-text">
<option value="<?php echo esc_attr( get_the_author_meta( 'gender', $user->ID ) ); ?>"><?php echo esc_attr( get_the_author_meta( 'gender', $user->ID ) ); ?></option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<span class="description">Please Select your Gender</span>
</td>
</tr>
</table>

<?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 ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( $user_id, 'contact', $_POST['contact'] );
update_user_meta( $user_id, 'gender', $_POST['gender'] );
}

Screen shot for How to add a field on the WordPress Users profile

Now go to your profile page You will able to see contact and gender field.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you have any Query / Suggestion