« WP No-bot Question plugin for WordPress | Home | Updated version of YASM v1.1.0 for Ubuntu »
Adding custom profile fields in WordPress (fully automatic)
By admin | October 17, 2011
Custom profile fields are additional custom-defined fields in the WordPress usermeta database that permit us to store additional information about a user. To add custom profile fields, add the following line to your functions.php in your theme folder:
include('customfields.php');
Now create a new file called customfields.php in the theme folder (the same folder as function.php) with the following contents:
<?php function get_extra_profile_list() { return Array( /* Add your custom fields, here like follows: 'slug_of_the_field_here' => 'Field name for display', */ 'address' => 'Address', 'favouritecolour' => 'Favourite Colour' ); } 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"> <?php foreach(get_extra_profile_list() as $key => $value) { ?> <tr> <th><label for="<?php echo $key; ?>"><?php _e($value); ?></label></th> <td> <input type="text" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo esc_attr( get_the_author_meta( $key, $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e("Please enter your $value."); ?></span> </td> </tr> <?php } ?> </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; } foreach(get_extra_profile_list() as $key => $value) { update_usermeta( $user_id, $key, $_POST[$key] ); } } ?>
Now if you go to “Users” under your administration and edit a user, the additional profile fields will show up at the bottom of the edit page.
If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!
Topics: PHP | 30 Comments »
December 9th, 2011 at 15:24
Thanx for the code, but how can I show the new field of the profile on the theme? but just the new field.
🙂
December 19th, 2011 at 23:03
The following snippet
Looks like it should do the job – but you probably need to tweak it 🙂
September 26th, 2012 at 11:22
that was very useful, helped me a lot
Thanks
December 13th, 2012 at 17:47
Hi,
Great information! I was wondering how to ensure that an Editor user role would be able to see the custom fields that I added? Right now I can only get Administrators to see them.
Thank you!
December 15th, 2012 at 20:24
@Cindy:
Change the “edit_user” to “edit_pages” in the line
if ( !current_user_can( ‘edit_user’, $user_id ) ) { return false; }
February 5th, 2022 at 19:18
… [Trackback]
[…] Find More on on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
February 9th, 2022 at 07:33
… [Trackback]
[…] Read More to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
February 26th, 2022 at 09:36
… [Trackback]
[…] Find More to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
March 31st, 2022 at 01:07
… [Trackback]
[…] Find More on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
April 6th, 2022 at 19:27
… [Trackback]
[…] There you can find 79856 additional Information on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
April 11th, 2022 at 07:33
… [Trackback]
[…] Information on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
April 21st, 2022 at 20:06
… [Trackback]
[…] Read More on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
April 30th, 2022 at 21:26
… [Trackback]
[…] Read More Information here on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
May 6th, 2022 at 16:13
… [Trackback]
[…] Info on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
May 27th, 2022 at 10:40
… [Trackback]
[…] There you will find 82102 more Info on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
June 5th, 2022 at 16:20
… [Trackback]
[…] Find More Information here to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
June 20th, 2022 at 20:23
… [Trackback]
[…] Info to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
July 11th, 2022 at 10:22
pg slot เว็บตรง แตกไว สล็อตน่าเล่น มีเกม สล็อต เว็บไซต์ตรง แจกเครดิตฟรี ให้เลือกเล่นล้นหลามมากมายแบบอย่าง เสมือนเลียนแบบแหล่งคาสิโนจากสถานที่จริงๆมาไว้บนโทรศัพท์เคลื่อนที่
August 28th, 2022 at 19:26
… [Trackback]
[…] Find More on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
August 31st, 2022 at 11:03
… [Trackback]
[…] Read More to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
August 31st, 2022 at 13:37
… [Trackback]
[…] Find More on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
September 16th, 2022 at 06:25
… [Trackback]
[…] Read More to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
September 28th, 2022 at 21:47
… [Trackback]
[…] Here you can find 18285 additional Info on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
November 24th, 2022 at 23:25
… [Trackback]
[…] Find More to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
November 26th, 2022 at 10:02
… [Trackback]
[…] Information on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
December 20th, 2022 at 07:52
… [Trackback]
[…] Find More to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
December 21st, 2022 at 08:52
… [Trackback]
[…] Find More on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
December 31st, 2022 at 15:24
… [Trackback]
[…] Read More here to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
January 18th, 2023 at 18:13
… [Trackback]
[…] Information to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
January 28th, 2023 at 20:50
… [Trackback]
[…] Here you can find 4065 more Info on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]