Changing username after registration to get around the issue of having duplicate emails?

You can try this skeleton plugin:

/** 
 * Plugin Name: Allow duplicate emails on registration 
 * Plugin URI: http://wordpress.stackexchange.com/a/125129/26350
 */

add_action( 'plugins_loaded', array( 'Allow_Duplicate_Emails_Registration', 'get_instance' ) );

class Allow_Duplicate_Emails_Registration
{
    private $rand = '';

    static private $instance = NULL;

    static public function get_instance() 
    {        
        if ( NULL === self::$instance )
            self::$instance = new self;

        return self::$instance;            
    }

    public function __construct()
    {
        // Generate some random string:
        $this->rand = '_remove_' . rand( 0, 99999);

        add_filter( 'user_registration_email', array( $this, 'user_registration_email' ) );
        add_action( 'user_register', array( $this, 'user_register' ) );

    }

    public function user_registration_email( $user_email )
    {
        // inject random string into the email
        $user_email = str_replace( '@', $this->rand . '@', $user_email );

        return $user_email; 
    }

    public function user_register( $user_id )
    {
        // retrieve the newly registered user object
        $user = get_user_by( 'id', $user_id );

        // remove the random string     
        $user_email = str_replace( $this->rand . '@', '@', $user->user_email );

        // update the email of the newly registered user
        $args = array(
            'ID'            => $user_id,
            'user_email'    => $user_email,
        );

        $result = wp_update_user( $args );  
    }

}

This plugin will add some random string to the email on registration. It’s done before the unique email checks and removed again afterwards, before the wp_new_user_notification() is activated in register_new_user(). So you should get the email notification sent to the unmodified email address.

You can then modify this further to your needs.


Previous answer:


I just got curious to see how one can get around the unique emails limitation:

Where are the checks for unique emails during registration?

a) Inside wp_insert_user() we have this check:

if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
                    return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) );

There is no explicit filter inside the function email_exists() that we could use to get around this. The 'WP_IMPORTING' constant might be way around this (but I’m not sure if it has some security problems)

b) Inside the register_new_user() function we have these lines:

} elseif ( email_exists( $user_email ) ) {
                $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );

    }

Workaround (Warning: not fully tested):

To get around the limitations in a) and b), we could try:

function custom_registration_errors( $errors ) 
{
    define( 'WP_IMPORTING', TRUE );

    unset( $errors->errors['email_exists'] );

    return $errors;
}

add_filter( 'registration_errors', 'custom_registration_errors' );

and maybe turn it off again:

function custom_user_register( $errors ) 
{
    define( 'WP_IMPORTING', FALSE );
}

add_action( 'user_register', 'custom_user_register' );

So this seems to let me register users with same emails, but it’s just some hack around the email checks, so be careful. I haven’t checked the implications of using WP_IMPORTING to get around the unique emails limitation. You should therefore dig further on that matter.

The Lost Password process will probably need some modifications?

And you will need to remove this during imports.

So hopefully this will get you started, but you should also try to reconsider your setup, maybe there’s a much better way to achieve your main goal 😉

Leave a Comment