« 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 | 8 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; }
November 9th, 2024 at 05:43
… [Trackback]
[…] Find More to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
November 12th, 2024 at 22:36
… [Trackback]
[…] There you will find 90550 more Information on that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]
November 13th, 2024 at 19:32
… [Trackback]
[…] Read More on to that Topic: compdigitec.com/labs/2011/10/17/adding-custom-profile-fields-in-wordpress-fully-automatic/ […]