0

I'm trying to display the current logged in user's info in a particular page i'm working on, and i've managed to display some of the user's info. But I decided to add a custom field to the user's profile such as "Agent Department", "Agent Contact" using this code:

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">
    <tr>
        <th><label for="agentdept"><?php _e("Agent Department"); ?></label></th>
        <td>
            <input type="text" name="agentdept" id="agentdept" value="<?php echo esc_attr( get_the_author_meta( 'agentdept', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("Enter Agent Department"); ?></span>
        </td>
    </tr>
    <tr>
        <th><label for="agentmobile"><?php _e("Agent Mobile"); ?></label></th>
        <td>
            <input type="text" name="agentmobile" id="agentmobile" value="<?php echo esc_attr( get_the_author_meta( 'agentmobile', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e("Enter Agent Mobile."); ?></span>
        </td>
    </tr>
    </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; 
    }
    update_user_meta( $user_id, 'agentdept', $_POST['agentdept'] );
    update_user_meta( $user_id, 'agentmobile', $_POST['agentmobile'] );
}

My question is, how do I get the value of these custom fields and display it on my front end? I really would appreciate the help. Thank you guys

0

Add this to the frontend and you should be able to adjust it to display whatever field you've added.

$user_info = get_userdata(get_current_user_id());
echo 'Username: ' . $user_info->user_login . "<br>";
echo 'User roles: ' . implode(', ', $user_info->roles) . "<br>";
echo 'User ID: ' . $user_info->ID . "<br>";
echo 'User Agent Department: ' . $user_info->agentdept . "<br>";
echo 'User Agent Mobile: ' . $user_info->agentmobile . "<br>";
| improve this answer | |
  • You are absolutely wonderful! Thank you for the help! – kitmin Jul 31 '19 at 7:14
  • You're welcome - Glad to help! – Louis S Jul 31 '19 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.