Re: Autogenerate or remove username
This basically works!:
function synchro_wp_usermeta($user_id, $password, $meta) {
global $bp, $wpdb;
$fullname = $meta[field_1];
$space = strpos( $fullname, ' ' );
if ( false === $space ) {
$firstname = $fullname;
$lastname = '';
} else {
$firstname = substr( $fullname, 0, $space );
$lastname = trim( substr( $fullname, $space, strlen($fullname) ) );
}
$lowercase = strtolower($fullname);
$autousername = str_replace(' ', '', $lowercase);
update_usermeta( $user_id, 'nickname', $fullname );
update_usermeta( $user_id, 'first_name', $firstname );
update_usermeta( $user_id, 'last_name', $lastname );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_login = %s WHERE ID = %d", $autousername, $user_id ) );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_nicename = %s WHERE ID = %d", $autousername, $user_id ) );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET display_name = %s WHERE ID = %d", $fullname, $user_id ) );
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_url = %s WHERE ID = %d", bp_core_get_user_domain( $user_id ), $user_id ) );
}
add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);
All the usernames in the database get updated to the new username autogenerated from fullname! Even the URL takes the new username.
Except the user_login in wp_signups. The signups table doesn’t have a user ID. I’ve tried adding an argument $user_email and this line:
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_login = %s WHERE user_email = %d", $autousername, $user_email ) );
But I get a missing argument error and all the entire column of user_logins is reset to the autousername of the latest user. Apparently ‘WHERE user_email = %d’ doesn’t work.
I’m not sure if I should waste much time on this. Is the user_login in wp_signups used anywhere? It appears in the emails, but those have to be rewritten anyway for email login.