As in the title, how to programatically change user’s login?
I wanted to use wp_insert_user
function, but it appears that when updating current user, it doesn’t change their username. Should I use $wpdb->update
for that? If yes, how would code for changing username look like? What consequences would changing user login have, given that WordPress API doesn’t allow changing usernames?
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.