• I set up a feature to enable the admin level user of my site to change a user’s user_login in the wordpress users table. The admin can enter a new user name from the user’s profile screen in the dashboard.

    My code uses wpdb to update the user_login field for the user’s row in the users table. However, wordpress also updates the user_nicename field, adding a “-2” to the end each time I update the user_login field.

    ex. nicename before update
    user-x

    ex. nicename after 1st update
    user-x-2

    ex. nicename after 2nd update
    user-x-2-2

    My update set clause only sets a value for user_login not user_nicename.

    Why does wordpress do this, and is there a way to prevent wordpress from updating the user_nicename field?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hey @hp3

    I do not have a solution, but I am facing the same issue (this is how I found your post).

    Would you have some clue on how to solve this?

    In my case, what I’m doing as a temporary solution is to go to PHPMyAdmin and manually edit every single nicename, which is obviously not a sustainable solution… 🙁

    Thread Starter hp3

    (@hp3)

    I have not yet found a solution. There must be some underlying reason why wordpress makes it difficult to update the user name, or maybe it is some kind of bug. Seems like there is something about preserving unique permalinks. I have not had a chance to look in the source code for the script that is generating these user nice names.

    Does anyone know which part of the source code does this?

    Hi there!
    Why don’t you update the user_nicename right after the user_login change?
    You can get it first using:
    get_currentuserinfo();
    And then save it with:
    wp_update_user(array('ID'=>$user_ID,'user_nicename'=>$current_user->data->user_nicename));

    In more steps:

    // Get current user info
    get_currentuserinfo(); // Initialize the object: $current_user
    
    // Do the change in the user_login
    .
    .
    .
    // Restart the user_nicename
    $userdata = array(
      'ID'  =>  $user_ID,
      'user_nicename'  =>  $current_user->data->user_nicename
    );
    wp_update_user($userdata);

    I hope this works for you.
    This doesn’t prevent wordpress from working funny, but it solves your problem.

    By the way, I don’t know how you are updating the user_login property (I guess you are using the wp_update_user function), but if you use the $wpdb object it doesn’t happen that ugly change in the user_nicename:

    // Call to the wpdb object
    $wpdb->update(
      'wp_users',
      array( 'user_login' => $new_user_login ),
      array( 'ID' => $user_ID )
    );
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wpdb update user_login also updates user_nicename’ is closed to new replies.