Compdigitec Labs

« | Home | »

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 | 22 Comments »

22 Responses to “Adding custom profile fields in WordPress (fully automatic)”

  1. Pancho Says:
    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.

    🙂

  2. admin Says:
    December 19th, 2011 at 23:03

    The following snippet

    <?php
    $id = $user->ID; /* the user ID here */
    $key = 'address'; /* Key here e.g. 'address' */
    echo esc_attr( get_the_author_meta( $key, $id ) );
    ?>
    

    Looks like it should do the job – but you probably need to tweak it 🙂

  3. Ahmed Says:
    September 26th, 2012 at 11:22

    that was very useful, helped me a lot

    Thanks

  4. Cindy Says:
    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!

  5. admin Says:
    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; }

  6. Firearms For Sale Says:
    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/ […]

  7. jokes up Says:
    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/ […]

  8. 토토사이트 추천 Says:
    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/ […]

  9. nova88 Says:
    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/ […]

  10. buy cvv for carding Says:
    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/ […]

  11. Buy Guns Online Says:
    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/ […]

  12. Chaturbate Tatiana_lp Says:
    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/ […]

  13. pgslot99th Says:
    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/ […]

  14. DevOps companies Says:
    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/ […]

  15. fake the best copy montblanc watches Says:
    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/ […]

  16. sbo Says:
    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/ […]

  17. สล็อต Says:
    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/ […]

  18. pg slot เว็บตรง แตกไว Says:
    July 11th, 2022 at 10:22

    pg slot เว็บตรง แตกไว สล็อตน่าเล่น มีเกม สล็อต เว็บไซต์ตรง แจกเครดิตฟรี ให้เลือกเล่นล้นหลามมากมายแบบอย่าง เสมือนเลียนแบบแหล่งคาสิโนจากสถานที่จริงๆมาไว้บนโทรศัพท์เคลื่อนที่

  19. clicking here Says:
    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/ […]

  20. Buy dmt online Melbourne Says:
    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/ […]

  21. Text Inmate Says:
    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/ […]

  22. maxbet Says:
    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/ […]

Comments