Adding Extra Fields to the WordPress User Profile
by c.bavota | Posted in Tutorials | 69 comments
Dec
10
2009
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'] );
}
?>



Hi there,
The code seems not to work in the latest version of wordpress.
Regards,
Stefan
It works for me with wordpress 3.0.4, the current latest version. Thanks so much for the tutorial! Stefan, did your functions.php already have code in it? If so, did you remove the first and last lines of code from the example before you pasted it in?
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
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?
You can sanitize the submitted values in the
save_extra_user_profile_fields()function.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
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?
great!! i have made it at last
I don’t have any issues with the latest wordpress
Does this work in 3.0.4? Thanks.
I tried and tried to put it in the comments and I can’t :l
I tried this:
I know that i’m doing it wrong.
Can you help me?
?> is :
$comment->comment_author
Appear all wrong :l
comment_author );
Sorry
@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
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
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
thanks so much ! i have been finding this for month ! thansk !
What is the hook to make this on the regiostration form?
[...] Adding Extra Fields to the WordPress User Profile | bavotasan.com [...]
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!
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?
Hi, where do the codes store using this custom fields? And how can I get the value of this fields in other pages?
The values are stored in the author meta. You can use the get_author_meta() function.
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!
Hi again
can you post an example of using a checkbox and storing/retrieving the checked/unchecked state?
Thanks!
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.
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
So nothing gets stored in to the database? How would I go about displaying this information for users in my theme?
I have tried this code, but how to display on website?
i try to display address but does not appear