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

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

  1. Pancho Says:
    December 9th, 2011 at 3:24 pm

    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 11:03 pm

    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 am

    that was very useful, helped me a lot

    Thanks

  4. Cindy Says:
    December 13th, 2012 at 5:47 pm

    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 8:24 pm

    @Cindy:

    Change the “edit_user” to “edit_pages” in the line

    if ( !current_user_can( ‘edit_user’, $user_id ) ) { return false; }

Comments