1. Home
  2. Troubleshooting
  3. Update User: user_login was not updated

Update User: user_login was not updated

If you try to update the user_login value for a user using the update_user webhook action, you realize that it does not work.
The reason for that is not the way how our plugin works, but that WordPress does not support the change of a user name within their wp_update_user function.
Yes, we know there is a separate value for user_login available, but on updating it, it gets ignored.

Is there a fix?

Yes, we created a code snippet that helps you archiving the update of a user login name. Down below you will find more information about that.

Here’s the manual:

  1. To get started, you need to define another argument in your update_user webhook call.
    The argument key is: do_action
    As the value, you need to set it to: change_login_name
  2. After that’s done, you need to include the following code into your functions.php file within your theme.
add_action( 'change_login_name', 'ironikus_change_user_login_name', 10, 3 );
function ironikus_change_user_login_name( $user_data, $user_id, $user_meta ){

	if( empty( $user_id ) ){
		return;
	}

	global $wpdb;

	$response_body = WPWHPRO()->helpers->get_response_body();
	$user_login = sanitize_title( WPWHPRO()->helpers->validate_request_value( $response_body['content'], 'user_login' ) );
	$unique_user_login = username_exists( $user_login );

	if( empty( $unique_user_login ) ){
		$wpdb->update($wpdb->users, array('user_login' => $user_login), array('ID' => $user_id));
	}
	
}

After you included the code, you can use the user_login value within the update_user webhook action as well to update the user name.

Please note: The user name is only updated when it does not exist for any other user. Changing the user_login causes as well a logout for the logged in user.

Updated on December 20, 2019

Was this article helpful?

Related Articles