How to programatically change username (user_login)?

I was sure that wp_update_user() should do this.

It even gets user_login as param, but it looks like it ignores it, when you set this param.

So this code looks OK, but it doesn’t work as you wish it did 🙁 :

wp_update_user(
    ['ID' => $user_id, 'user_login' => $new_login] 
);

You have to call custom SQL query to update user_login:

global $wpdb;
$wpdb->update(
    $wpdb->users, 
    ['user_login' => $new_user_login], 
    ['ID' => $user_id]
);

It works OK and I don’t think it has any serious consequences, because WP uses users ID to assign posts/comments (and so on) to user.

The only problem I can think of is that when this user is currently logged in, he will be logged out after user_login change.

Leave a Comment