Dec
10
2009
Adding Extra Fields to the WordPress User Profile
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,
This looks very nice, but I have two additional newbie questions:
1. How, in a private blog, where the members cannot register but are registered by the admin, can they nevertheless update their profile with the additional fields?
2. Once the extra user fields are in the database, how to display (selected) fields in a table form on a page?
Best regards,
Jan
1. Once a member is registered then can access their profile in the admin.
2. Use the get_the_author_meta() function.
Thanks for the great info!
Couple of questions…
1. How to implement radio buttons?
2. How to add the custom fields to the registration page?
To implement a radio button you just need to use a radio input like so:
Then you just need to add a new update_meta call like so:
Adding to the registration page is a bit more complicated but luckily there are some plugins available for that. Check out the directory.
Thanks for the quick response!
My radio button problem was I was not displaying the checked value, so each time the user profile was re-saved the field was deleted. The following code solved the issue:
Used Register Plus to implement a custom registration page.
Thanks again for your help!
Code did not paste well trying again:
Opening and closing php = respectively
Sorry for all the responses. Still did not get it to paste correctly. Let me know if you’d like the code for users who may run into this issue in the future and maybe I could send it via email.
Thanks for writing this. I’ve had a few issues with the Register Plus plugin, but this seems way easier to implement and customize. I can’t, however, find any reference to the ‘edit_user_profile_update’ action in the WordPress codex. Do you know if its documented somewhere else, or explains somewhere in the WP code?
I have put the code into my functions.php file and it shows up fine in the profile. This issue is when I edit the profile and hit the update profile button, I get a blank page and (profile.php). If I refresh the page, the profile comes back up.
The only different I see between the default action (with the code not in place) and the new action is an some HTML after the profile.php
?updated=true&wp_http_referer
Any insight into what is going wrong?
Thanks!
-joe
What version of WordPress are you using?
great little script, works flawlessly in 2.9.2. Displayed perfect in the user profiles and was easily able to add additional fields to the script. In addition, works perfectly with the_author_meta() to display the info on the site.
thank you very much, garrett
Hello just thought i would tell you something.. This is twice now i’ve landed on your blog in the last 3 days searching for totally unrelated things. Spooky or what? If you liketo swap the links with us please let me know.
Hello this is a great tutorial saved me a lot of time. I am just having one issue when put something in the custom textarea which i added to the profile and click update profile it does not get saved. If someone can help me with this problem i would appreciate it. Thank You.
@ c.bavota
hello again, script still working excellent, one question however, I am trying to allow only user level 8 and above to be able to update the user meta. I currently have it working by adding
The problem is I have to add it before each update_usermeta. It actually works exactly how I want this way but its kind of ugly, would you know of a cleaner method?
thanks very much
I think for that, you might want to use a readonly parameter on the inputs so that users below level 8 can’t change the inputs.
Then add the $readonly variable into each input field like so:
Now, if a user is lower then level 8, the $readonly variable will have a value and those input fields will not allow change.
Very usefull post, thanks!
How insert a new role for the users?
Example: User roles: student, professor, coordinator.
how?
thanks!
@c.bavota
thanks very much for the prompt reply, that is quite refreshing in itself. i tried your suggestion to no avail. i have to admit i did not spend a lot of time on it but i did a little reading and i think “readonly” is strange in php, from what i can tell it would need its own class defined, of which i found a couple examples but have not had the time to try them out or attempt myself. From what i read however, “readonly” is somewhat of an anomaly in php. If you feel like messing with it that would be cool and i would love to see your resolution but certainly don’t pull your hair out over it since, as i said, my method works perfectly, just quite ugly!
thanks very much again and i will check back!
hi
i’ve been trying for long to figure out the best way to do this. and i found a plugin but it’s pretty complicated to use for a non-pro, as me. so, first, thanks.
and second, i’m still having a problem. i believe it can be with adding other function to functions.php, where i’m also defining the sidebars i’m using. so i’m getting this error, when trying to save the info on the new fields for a user. this is the error:
Warning: Cannot modify header information – headers already sent by (output started at /home7/layabozi/public_html/test/wordpress/wp-content/themes/Layabozi Reload/functions.php:44) in /home7/layabozi/public_html/test/wordpress/wp-includes/pluggable.php on line 868
I have no idea why it’s the pluggable.php reacting on this, and on functions line 44 is where your code begins.
do u think you can help me? or should i try to figure out that plugin to solve my nightmares? :/
anyway, thanks again
I think you can help me. Your work is great I want to know how to add a form in the members profile section that post to the admins profile area. Or do you know of any plug in that can do this.
Thanks
What exactly do you want to add?
I would like to add extra fields to the members profile section that will post the information to the dashboard for the admin.
Fields are as listed -
First name:
Last name:
Address: City: State: Zip:
Email:
Email Conformation:
Check-in date:
Checkout Date:
Event name:
Rate Paid:
Reservation made By:
I want it to post in the dashboard area for the admin. In members order. Most important is the email address. The purpose is to send out news letters and coupons to these email addresses. All my subscribers will be business owners only. Some sort of search would be a good idea to sort threw this info.
Thank You
You should probably use a mailing list plugin like MailPress to maintain your subscribers.
As for the extra fields on the profile page, I suggest adding them like in the code above.
great article and some great answers here.
maybe here is someone who can help me… i am looking for a solution or tip to store a timestamp (user profile last modified at:) in the user_meta table every time a user updates his profile. in theory i thought a hidden field with a date function could solve my problem, but i really stuck at this point.
the best solution would be that all user profile informations will send per mail to the admin, if the user submit the profiles changes. If anybody has tips to solve that or has a working solution… please let me know. i would also pay for that… thanks
You can hook into an action when the user profile is saved and either add a field for the date or email the admin. If you are interested in having me build a plugin for you let me know by emailing me through the contact link above.
Thanks for your quick response. I am interested. Did you get my private message? Thanks for your time and maybe help.
great post! in some wordpress i work, but in one of my websites, it was this erro: Warning: Cannot modify header information – headers already sent by (output started at /home/jc2esportes/www/site/wp-content/themes/organic_magazine/functions.php:111) in /home/jc2esportes/www/site/wp-includes/pluggable.php on line 890
Hmm. What is line 111 in your functions.php file?
Hello, thanks for the article. I’ve been wondering how to force certain custom fields to be marked as required during the registration process?
Thank you for reply.
Hi Richard,
did you find a way to make certain extra fields required/??
regards
Can you recommend some plugins for enabling me to add new fields to the user registration page?
I have never used any, but you can probably search the WordPress.org plugin directory to find one that works for you.
it’s compatible with wordpress 3 ?
thanks, very good post dude
Yes it is
Just i Want to say THANK YOU
I have tried to add checkboxes and select menus, but they always clear and cant seem to figure how to display on page.
I need days off the week sun-sat and times 00hours to 23 hours
anyone have sample of code for functions and author page would be helpful. thank you
Great post. I have been tackling this issue for a few hours, all my hacks were not playing friendly with get_author_meta(). I will implement your code and save myself another few hours of aggravation.
The WordPress community cannot be beat for finding a solution when your brain is tired!
I want to let the users specify whether each extra field should be public or private – the public fields would then be shown in a user directory. How can I do this?
Hmm. Not too sure about that. What exactly are you trying to do and why?
I have not tried this yet, but couldn’t this be implemented as a plugin so as to avoid the loss of data/functionality when one upgrades the site’s theme?
Okay, and now having said that – anyone want to throw out ideas on how, exactly, to achieve that?
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?