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

8 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. บับเบิ้ล Says:
    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/ […]

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

  8. World Lotto แอปพลิเคชั่นแทงหวย ครบวงจร Says:
    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/ […]

Comments