Dec
10
2009

Adding Extra Fields to the WordPress User Profile

by   |  Posted in Tutorials  |  68 comments

By default WordPress offers some great options. Whenever a member joins, they have the ability to add more information about themselves, such as a Web site URL, a short bio and their AIM. You might require a bit more info from your members and creating extra fields in the user profile is pretty straightforward.

With the following code, you can add some extra fields asking your members for their address. Just add the code t your theme’s functions.php file, or create a functions.php file if you don’t already have one.

<?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="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_user_meta( $user_id, 'address', $_POST['address'] );
update_user_meta( $user_id, 'city', $_POST['city'] );
update_user_meta( $user_id, 'province', $_POST['province'] );
update_user_meta( $user_id, 'postalcode', $_POST['postalcode'] );
}
?>

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...

Tags: , , , , , , , , ,

Short URL: http://bit.ly/ckXZzT

Discussion 68 Comments

  1. Jim Carey on December 10, 2010 at 5:22 pm

    Hi,

    would like to have a custom field (checkbox) for each user that the user can see (or maybe not even see) but NOT edit – but admin can see AND edit that I can then interrogate in another plugin. Admmin could set this on registration maybe and update subsequently if needed (but user can’t – or even can’t see)

    Does anyone have any suggestions on how I would accomplish this ?

    Jim

  2. Fabio Salomoni on December 16, 2010 at 4:18 am

    Hi, such a very interesting article! I have a problem: how to implement a way to check values that users insert and block the procedure in case some values miss or are wrong?

    • c.bavota on December 16, 2010 at 1:05 pm

      You can sanitize the submitted values in the save_extra_user_profile_fields() function.

  3. Joanna on December 23, 2010 at 8:40 am

    hi and thanks a lot for this information,
    i have add a checkbox in user profile page that save the multi selected options in database but i try to list this selections in a template file but it returns an Array as text,
    how i can call the selected checkboxes?

    thanks a lot!!
    Joanna

    • c.bavota on December 23, 2010 at 11:39 am

      Have you assigned your checkboxes a value? If not, they will only have a value of “on” if they have been checked, and nothing if not. Are you using the the_author_meta() to retrieve the data? Have you added the User ID?

  4. jack on December 30, 2010 at 11:17 am

    great!! i have made it at last

  5. Definition on January 3, 2011 at 9:03 pm

    I don’t have any issues with the latest wordpress

  6. Mike Bundrant on January 8, 2011 at 2:34 am

    Does this work in 3.0.4? Thanks.

  7. Alvaro on January 15, 2011 at 10:29 am

    I tried and tried to put it in the comments and I can’t :l
    I tried this:

    comment_author ); ?&gt;

    I know that i’m doing it wrong.
    Can you help me?

    • Alvaro on January 15, 2011 at 10:30 am

      ?> is :
      $comment->comment_author

  8. Alvaro on January 15, 2011 at 10:31 am

    Appear all wrong :l

    comment_author );

    Sorry :(

  9. T-Leo on January 18, 2011 at 6:00 pm

    @C Bavota or anyone who had this issue solved, when I update the user information, the field box remains blank. Was my data saved or not?

    Much thanks,
    T-Leo

  10. KB on January 24, 2011 at 6:17 am

    Hi,

    Great article – just what I was looking for… BUT

    I don’t think the data is being saved as the fields are blank when you return to your profile to edit.

    I’m using wordpress 3.0.4 and have copied the code into my functions.php file. The Edit profile forms show the extra fields as expected. But go blank once I hit update?

    any clues please?

    KB

  11. KB on January 24, 2011 at 6:58 am

    Arh figured out the errors of my way :-)

    It does work with WP 3.0.4.

    I’d renamed some of the fields from the copied code and forgot to change the “name” value of the Input. Basically make sure name, id and all the variables match up if you change the sample code.

    One thing I did find out function “update_usermeta()” is now depreciated and WP advise to use “update_user_meta()”

    KB

  12. jam on March 1, 2011 at 10:18 am

    thanks so much ! i have been finding this for month ! thansk !

  13. Dofollow search engine on March 7, 2011 at 4:31 am

    What is the hook to make this on the regiostration form?

  14. get your ex on March 11, 2011 at 5:01 pm

    [...] Adding Extra Fields to the WordPress User Profile | bavotasan.com [...]

  15. wjima on March 11, 2011 at 11:11 pm

    First,I’m very thank you. but i have a question. How to add judgment if users input, meanwhile the input is the correct format
    ?

    Thank you!

  16. Ken on March 15, 2011 at 2:49 am

    Nice little code. I was wondering if it is possible to add the extra fields at the point when you click on ‘Add New User’?

    I want to add a field for company name and address, but want to fill it out for the user first and then they can only read it if they view their profile not edit it.

    Is this possible with the function.php?

  17. Aris on April 24, 2011 at 9:03 pm

    Hi, where do the codes store using this custom fields? And how can I get the value of this fields in other pages?

    • c.bavota on April 25, 2011 at 9:58 am

      The values are stored in the author meta. You can use the get_author_meta() function.

  18. Nigel Minchin on April 28, 2011 at 12:49 pm

    Great article very useful. To avoid the ‘Cannot modify header information – headers already sent by …’ error you must make sure no spaces before first tag.

    Thanks again!

  19. Nigel Minchin on April 28, 2011 at 1:10 pm

    Hi again

    can you post an example of using a checkbox and storing/retrieving the checked/unchecked state?

    Thanks!

  20. Grant on April 28, 2011 at 11:33 pm

    Thanks man, this code helped me add some fields in the user profile that I need. I was giving myself a headache trying to figure it out.

  21. Nigel Minchin on May 3, 2011 at 12:35 pm

    Hi again

    I need to add fields in a custom user registration page, then show in the admin. Any idea how to add in as part of the registration process?

    Thanks

    Nigel

  22. Atlante on May 26, 2011 at 4:58 pm

    So nothing gets stored in to the database? How would I go about displaying this information for users in my theme?

  23. zulsdesign on May 28, 2011 at 5:55 am

    I have tried this code, but how to display on website?
    i try to display address but does not appear