Saving custom profile fields

I’m adding a custom profile field to users of a specific role, like this :

function add_custom_profile_fields( $fields ) {

    // get current user ID
    $user = new WP_User( $_GET['user_id'] );

    // get current user role
    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
        foreach ( $user->roles as $role ) {

            // filter roles
            if ($role == "paying_member"){
               $fields['paypal_account'] = 'Paypal account';        

    return $fields;

The problem is, the field’s value doesn’t get saved. When I login as admin an I edit a user’s profile. It somehow has to do with the fact that I’m filtering by user role, because when I remove that part, the values get saved perfectly.

EDIT : I think maybe the whole method is wrong, I’m going to try this instead.

3 Answers

Ok, I was doing it wrong, here’s a working solution, based on Justin Tadlock’s tutorial.

/* Add custom profile fields (call in theme : echo $curauth->fieldname;) */ 

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>

    <?php if(user_can($user->ID, "paying_member")) : ?>

        <h3>Extra profile information</h3>

        <table class="form-table">

                <th><label for="paypal_account">Paypal</label></th>

                    <input type="text" name="paypal_account" id="paypal_account" value="<?php echo esc_attr( get_the_author_meta( 'paypal_account', $user->ID ) ); ?>" class="regular-text" /><br />
                    <span class="description">Please enter your Paypal account.</span>


    <?php endif; ?>

<?php }

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    /* Copy and paste this line for additional fields. Make sure to change 'paypal_account' to the field ID. */
    update_usermeta( $user_id, 'paypal_account', $_POST['paypal_account'] );


The main addition to his code, is this line of code:

<?php if(user_can($user->ID, "paying_member")) : ?>

Which displays the custom fields only for users with the role of “paying_member” and admins.

Leave a Comment