0

When a user updates their email address we are seeing that their old email address becomes unavailable to register. This is undesirable.

It appears this is based on the user_login and user_nicename are not updated with user_email.

To fix this we are planning to:

$wpdb->update($wpdb->users, array('user_login' => $new_user_login, 'user_nicename' => $user_nicename), array('ID' => $user_id));

This must be a common issue other people are seeing. So I just wanted to ask, is this the correct way to fix this?

3
  • Are you using plugins for this logic? I tried to change a user email to a new one and then register a new user with the previous email and encountered no problems Commented Feb 10, 2021 at 15:21
  • 1
    I just tried on a localhost and I got the same result as @Buttered_Toast - what WP version are you using?
    – Dave White
    Commented Feb 10, 2021 at 15:33
  • What do the user nicename and login have to do with the email changing? I don't understand where the connection is being drawn from. I also don't understand why $wpdb->update is being used instead of wp_update_user, a direct database query bypasses all the caching systems, they won't have the chance to flush themselves.
    – Tom J Nowell
    Commented Feb 10, 2021 at 15:59

1 Answer 1

1

There is nothing in plain vanilla WordPress that would result in what you described.

It would seem that your problem stems from customizing your process (either by plugin or by your own customization) to populate the user_login with the user's email address to avoid having both a username and email. If it's a custom process that you put in place yourself, then the issue is a result of not thinking through all of the possible issues based on circumventing the core WP process.

When doing something like this, the problem is when the user changes their email address, you also have to account for their username changing. Both email and username must be unique values (display name does not need to be unique). If the user changes their email but the user_login value is their previous email, the validation for unique username will return an error when trying to register the previous email.

It's not a good idea to simply populate the username with the email address. WP intentionally does not have a built in process for changing usernames, and so you have to circumvent that process with a direct db update. Any time you try to do something to "workaround" a core part of WP, you need to ask yourself two questions:

  1. Is this a good idea?
  2. Do I really need to do it this way?

If you simply MUST do away with username altogether, a better method is to come up with a way to create a username value from the email address (like extracting the first part) rather than having it be the actual email. BUT... then you still need an additional process during validation to make sure that your autopopulated pseudo username results in a unique value (like if "john" already exists, add "1" to it).

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.