WordPress.org

Forums

[resolved] Current user display_name does not update (2 posts)

  1. michalmusial
    Member
    Posted 2 years ago #

    Hello,

    I would like to update user's display_name and first_name.
    I am doing it this way:

    $current_user = wp_get_current_user();
    $update = array(
    'ID' => $current_user->ID,
    'first_name' => $field_value,
    'display_name' => $new_display
    );
    wp_update_user( $update );
    
    $current_user = wp_get_current_user();
    echo $current_user->user_firstname . ' ' . $current_user->display_name;
    exit(); // all these are in AJAX request

    The 'echo' line shows, that first_name is updated, but display_name - not. In the database (wp_users) both fields are updated. The display_name is echo'ed correctly after reload of the page.
    Why does $current_user->display_name contain old value after wp_update_user() and wp_get_current_user() invocations, while $current_user->user_firstname contains the new one?

    I would appreciate the answer.

    Regards,
    Michal Musial

  2. michalmusial
    Member
    Posted 2 years ago #

    Function:
    wp_get_current_user()
    does not taken the data from the database. It uses global variable: $current_user.

    So after wp_update_user() I used:

    $user_ID = get_current_user_id();
    $current_user = get_userdata( $user_ID );

    and it is fine now.

    Resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic