3

I need to keep in sync the username and the email of my users. I don't know if there is a better way so I have tried running this code right after the user updates his profile data:

global $wpdb;
$wpdb->update(
    $wpdb->users,
    array('user_login' => $email),
    array('ID' => $user->ID)
);

but this way the auth cookie becomes invalid and the user is logged out.

I have also tried forcing the creation of the auth cookie right after the update, like this:

wp_clear_auth_cookie();
$user = get_user_by( 'id', $user_id ); 
if( $user ) {
    wp_set_current_user( $user_id, $user->user_login );
    wp_set_auth_cookie( $user_id, true );
    do_action( 'wp_login', $user->user_login );
}

but with no results.

  • Is there a technical reason why they must be the same in the database or is it only for presentation? – Evan Mattson Dec 30 '16 at 12:48
  • not really. WP allows to login using the email also, so it's not exactly a REAL issue. The problem is that this website is communicates with another web app and I have to replicate the users... I just want to keep everything in sync to avoid potential issues. – MizRaeL Dec 30 '16 at 12:53
  • Note that bypassing WP internal functions to set user login to email address will break other WP functions like lost/reset password. Plus email address can contain special characters like spaces percentages quotes etc. Its best to sanitize the email address before setting to user login. – user42826 Dec 30 '16 at 18:11
  • now THAT is interesting. I haven't thought about that, thank you very much! – MizRaeL Dec 31 '16 at 9:04
0

Try hooking in as soon as the user is created or updated.

<?php

function wpse_250785_sync_user_login_with_email($user_id) {
    $user = new WP_User($user_id);

    if ($user->user_login != $user->user_email) {
        $user->user_login = $user->user_email;
        wp_update_user($user);
    }
}
add_action('user_register', 'wpse_250785_sync_user_login_with_email');
add_action('profile_update', 'wpse_250785_sync_user_login_with_email');

Disclaimer: Untested, but should work ;)

  • If I remember correctly, wp_update_user() won't update the user_login field, that's why I am running $wpdb->update(). BTW, I'll try the 'user_register' hook... I am pretty sure I have already done this but I'll try again :D – MizRaeL Dec 30 '16 at 13:19
  • I don't see any thing in there that would prevent that from being updated. It is only the UI which doesn't give a user the means to change it. – Evan Mattson Dec 30 '16 at 13:30
  • I don't want to sound harsh, but this is right from the WP docs ( codex.wordpress.org/Function_Reference/wp_update_user ) : user_login: "A string that contains the user's username for logging in. Please note that the function cannot alter this field, since WordPress does not allow usernames to be changed." – MizRaeL Dec 30 '16 at 14:40
  • My mistake, thanks for going easy on me. I was actually referencing the developer doc here developer.wordpress.org/reference/functions/wp_update_user which doesn't mention that. That's quite strange that it isn't allowed without using wpdb directly like that. Not the strangest thing in core though, I'm sure ;) – Evan Mattson Dec 30 '16 at 23:07
  • no problem, I spent two days banging my head on it, that's why I was 99.9% sure :D – MizRaeL Dec 31 '16 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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