Add More Extra Informations Or Fields To WordPress User Profile
Posted by in PHP May 29, 2011 31 Comments

This tutorial will show you how to add some extra information for your WordPress user profiles such as: Facebook URL, Twitter, Date Of Birth, Phone, Address, City, Province, Postal Code. Of course, you can add as much as extra fields as you want.

As you know, WordPress allows people to register to your site or the admin can add new user in the Dashboard with just some simple information related to their Name, Contact Info and Biographical Info.

WordPress Default User Information

WordPress Default User Information

However, in some case, you might need to know more about your users and ask them to provide some additional informations. To do that, just copy following codes below and paste them to the functions.php file in your theme directory (ex: “/wp-content/themes/twentyten/” where “twentyten” is your current theme name) or you have to create a new one if you don’t have it.

<?php
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="facebook"><?php _e("Facebook URL"); ?></label></th>
<td>
<input type="text" name="facebook" id="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your Facebook URL."); ?></span>
</td>
</tr>
<tr>
<th><label for="twitter"><?php _e("Twitter"); ?></label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your Twitter Username."); ?></span>
</td>
</tr>
<tr>
<th><label for="dob"><?php _e("Date Of Birth"); ?></label></th>
<td>
<input type="text" name="dob" id="dob" value="<?php echo esc_attr( get_the_author_meta( 'dob', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your Date Of Birth, ex: 13/06/1983"); ?></span>
</td>
</tr>
<tr>
<th><label for="phone"><?php _e("Phone"); ?></label></th>
<td>
<input type="text" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your Phone Number."); ?></span>
</td>
</tr>
<tr>
<th><label for="address"><?php _e("Address"); ?></label></th>
<td>
<input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your address."); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e("City"); ?></label></th>
<td>
<input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your city."); ?></span>
</td>
</tr>
<tr>
<th><label for="province"><?php _e("Province"); ?></label></th>
<td>
<input type="text" name="province" id="province" value="<?php echo esc_attr( get_the_author_meta( 'province', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your province."); ?></span>
</td>
</tr>
<tr>
<th><label for="postalcode"><?php _e("Postal Code"); ?></label></th>
<td>
<input type="text" name="postalcode" id="postalcode" value="<?php echo esc_attr( get_the_author_meta( 'postalcode', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your postal code."); ?></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 ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
 
update_usermeta( $user_id, 'facebook', $_POST['facebook'] );
update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
update_usermeta( $user_id, 'dob', $_POST['dob'] );
update_usermeta( $user_id, 'phone', $_POST['phone'] );
update_usermeta( $user_id, 'address', $_POST['address'] );
update_usermeta( $user_id, 'address', $_POST['address'] );
update_usermeta( $user_id, 'city', $_POST['city'] );
update_usermeta( $user_id, 'province', $_POST['province'] );
update_usermeta( $user_id, 'postalcode', $_POST['postalcode'] );
}
?>

The PHP codes above will add a new table called “Extra profile information” to the Dashboard User Profile page which includes all extra information you want to add in. When you press Update Profile button, it will update all the extra fields one by one by calling the update_usermeta() function along with all default WordPress information.

WordPress Extra profile information

WordPress Extra profile information

+ Download my current functions.php file in Twentyten’s theme. I placed the PHP code above at the bottom of the file.

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • Pingback: Create Custom Update Profile Page For WordPress Users | 4 Rapid Development

  • Pingback: Create Custom Update Profile Page For WordPress Users | 4 Rapid Development

  • Vincen

    Hi! Thanks for this great article.
    I’ve some questions :
    I’m trying to build a online shop with a wordpress and some plugin, and let the user to register to my site with address information, telephone number, etcc.
    What have I do to create a register page for the user with all the information (address, telephone, etcc) ? Then I simply want when they login, and they buy an article, the checkout form is filled automatically with the information present in their profile : how can I do that? ..so how can I get the information from their account after the login? And what can I do to verify if they are logged in? I’m neebie about wordpress and I’ve haven’t found useful information on the registration/login wordpress codex.
    Thanks for the help!

    Vince.

    • Na

      Hi Vince,
      I think to get familiar with WordPress, the first thing you need to install & play with some WordPress plugins. In your case, your should install the Register Plus plugin: http://wordpress.org/extend/plugins/register-plus/ that can be suitable for your registration process.
      Hope it helps.

  • http://www.register-domainname.in Register domain name

    This code really works for me.

    • hoanhuynh

      Thanks for checking it out.

  • benetic

    thanks a lot for this very useful post that makes it possible to extend user profile withour using any plugin. I have tryed your hack successfully for text-type extra fields. However, i couldn’t manage coding other types of fields (for example radio type). is there any chance you could give me a clue to make it properly ? thanks

    • hoanhuynh

      Hi benetic,

      For a radio type example, let’s say Gender, you need to:
      + Add these code lines somewhere in the Update profile table in the page-update-profile.php:
      ——————-
      <tr>
      <td>Gender</td>
      <td>
      <?php
      $gender = esc_attr( get_the_author_meta( 'gender', $userdata->ID ) );
      if($gender == "male")
      $male_status = 'checked="checked"';
      else if($gender == "female")
      $female_status = 'checked="checked"';
      ?>
      <input type="radio" value="male" <?php echo $male_status; ?> id="gender" name="gender" />Male <input type="radio" <?php echo $female_status; ?> value="female" name="gender" id="gender"/>Female </td>
      </tr>
      ——————-
      + Add this to save_extra_user_profile_fields function in the functions.php (in your template root folder):

      update_usermeta( $user_id, ‘gender’, $_POST['gender'] );

      That’s all. It should work as dream :)

      • http://www.google.com/ Micheal

        Grazi for mkanig it nice and EZ.

      • http://www.bing.com/ Kamryn

        No complaints on this end, smilpy a good piece.

    • http://www.bing.com/ Essy

      Whoever edits and publishes these arctiles really knows what they’re doing.

  • benetic

    I will try and come back to you with feedback. Thanks a lot anyway !

  • benetic

    hello hoanhuynh,
    It took some times but i managed to code my page with the code your provided : it’s a great hack your are giving here !!! As I was completely seduced, i’ve tried to adapt it also for the checkbox fields using the following :

    ID ) );
    if($public_email== "yes") {$email_status = 'checked="checked"';
    } else {$email_status = '';
    } ?>
    <input type="checkbox" value="yes" name="public_email" id="email_statut"/>

    Now i have the following issue : when the box is checked, field is correctly updated (with the ‘yes’ value) , but when the box is uncheked, the field is updated with an empty value (instead of ‘no’). Could you tell me what i’m doing wrong ? (i promise it’s my last question)

    • benetic

      something got wrong with the tag in my previous msg :
      -----------------------
      $public_email= esc_attr( get_the_author_meta( 'public_email', $userdata->ID ) );
      if($public_email== "yes") {$email_status = 'checked="checked"';
      } else {$email_status = '';
      } ?>
      <input type="checkbox" value="yes" name="public_email" id="email_statut"/>
      ---------------------------------

      • hoanhuynh

        Hi benetic,

        You’re welcome to ask. I’ll try with my best to help.

        Please change a little bit in your functions.php file for updating the ‘public_email’ property.

        I guess you’re programming:

        ————————————–
        update_usermeta( $user_id, ‘public_email’, $_POST['public_email'] );
        ————————————–

        it should be changed to:

        ————————————–
        $public_email = $_POST['public_email'];
        if($public_email == “”)
        $public_email = “no”;
        update_usermeta( $user_id, ‘public_email’, $public_email );
        ————————————–

        Hope it solves your headache :)

        • http://www.google.com/ Nelle

          I found myself nodidng my noggin all the way through.

        • http://www.facebook.com/ Lele

          I just hope whoever witres these keeps writing more!

  • vino

    Hi!
    Thanks to this article..
    i need some help.
    how to add avatar in user profile and how to display avatar in logout content…

  • http://www.thingk.co.nz Adam

    Hi Great article.
    Is there anyway that the information displayed in the user profiles can be displayed in a page? like a members list?

    cheers
    Adam

  • http://myuntangledweb.com Joanne Becotte

    This post was just what I was looking for but am having some problems adding Google+ to the user profiles as outlined above. Have you found any issues with that?

  • http://wpcreative.info AnLe

    Hi,
    Any way to make the extra fields displayed above the others ?

  • http://www.purgexonline.com Purgex Purging

    Great article, was just what I needed, really appreciate the full explanations.

  • http://www.bing.com/ Jodi

    If I communicated I could thank you eoungh for this, I’d be lying.

  • http://www.google.com/ Bubber

    A good many vallubaes you’ve given me.

  • pree

    Hi thanks for gave for Great article

  • Bidrohi462

    That was really wonderful ……… I think I’m gonna use this in the website I am developing right now…………

    There is one problem :: how can I use these new data to create a custom profile page? A quick reply could really help me a lot.

  • Kuandi2001

    Hi, this is an amazing trick, however is there anyway to move the Extra Fields above the About yourself section?

  • http://www.facebook.com/imran.husain.3760 Imran Husain

    I got an error in the functions.php file when I tried to load the users page after copying and pasting at the end of file in the functions.php file. Where do I paste it?