To work around the "that email address has already been used" error, we can create a plugin that effectively bypasses the check. The plugin will work in three parts, utilizing three different hooks.
pre_user_login
filters a username after it has been sanitized. We'll use this hook to grab the user, of particular interest is the email.
pre_user_email
filters a user’s email before the user is created or updated. We'll use this hook to modify the email to some random characters.
user_register
fires immediately after a new user is registered. We'll use this hook to manually update the user email after the user is registered.
add_filter( 'pre_user_email', [ new wpse_212671(), 'pre_user_login' ] );
class wpse_212671 {
protected $user;
public function pre_user_login( $user ) {
$this->user = $user;
if( isset( $user[ 'ID' ] ) || ! get_user_by( 'email', $user[ 'user_email' ] ) {
return $user;
}
add_filter( 'pre_user_email', [ $this, 'pre_user_email' ] );
add_action( 'user_register', [ $this, 'user_register' ] );
}
public function pre_user_email( $email ) {
return $this->generate_random_string();
}
public function user_register( $user_id ) {
global $wpdb;
$table = $wpdb->prefix . 'users';
$wpdb->query( $wpdb->prepare(
"UPDATE %s
SET user_email = %s
WHERE user_login = %s",
$table,
$this->user[ 'user_email' ],
$this->user[ 'user_login' ]
) );
}
//* Code slightly modified from http://stackoverflow.com/a/13212994/6077935
protected function generate_random_string( $length = 40 ) {
return substr( str_shuffle( str_repeat(
$x='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
ceil( $length / strlen( $x ) )
) ), 1, $length );
}
}