最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

I want to change username and email after a user has been created and sent an email. This is because I want!! (yes, you read correctly) to have duplicates in my database. In normal situations this is not a good option but this I just to have to at the moment.

//Workaround-Set registered unix-timestamp before each new user-login and user-email when saving new users 
//so we can use duplicate usernames and emails
function save_user_fields($user_id) {
    global $wpdb;
    $wpdb->query( 
            $wpdb->prepare( 
                    "
                    UPDATE $wpdb->users 
                    SET user_email=CONCAT('registered-%s',user_email),user_login=CONCAT('registered-%s',user_login), 
                    WHERE ID = %s AND user_login LIKE 'VT2014%' AND user_email NOT LIKE 'registered%' 
                    LIMIT 1 
                    ",
                    time(), time(),$wpdb->insert_id
            )
    );
}
add_action('user_register', 'save_user_fields'); //When adding new users

I want them to be changed AFTER they have been saved and after an email-confrimation mail has been sent to actual type-in-mail by user.

With above code Wordpress says that there are already an email adress in the database and stops there... ...and that is actuall what you want in most cases

I've also tried this:

function changeusername($user_id) {
    global $wpdb;
    $wpdb->query( 
            $wpdb->prepare( 
                    "
                    UPDATE $wpdb->users 
                    SET user_email=CONCAT('registered-%s',user_email),user_login=CONCAT('registered-%s',user_login), 
                    WHERE ID = %s AND user_login LIKE 'VT2014%' AND user_email NOT LIKE 'registered%' 
                    LIMIT 1 
                    ",
                    time(), time(),$wpdb->insert_id
            )
    );
}
    add_action('register_post', 'changeusername'); //Just before saving new user to database

I want new users be changed AFTER they have been saved and after an email-confrimation mail has been sent to actual typed-in-mail by user. How could I solve that?

I want to change username and email after a user has been created and sent an email. This is because I want!! (yes, you read correctly) to have duplicates in my database. In normal situations this is not a good option but this I just to have to at the moment.

//Workaround-Set registered unix-timestamp before each new user-login and user-email when saving new users 
//so we can use duplicate usernames and emails
function save_user_fields($user_id) {
    global $wpdb;
    $wpdb->query( 
            $wpdb->prepare( 
                    "
                    UPDATE $wpdb->users 
                    SET user_email=CONCAT('registered-%s',user_email),user_login=CONCAT('registered-%s',user_login), 
                    WHERE ID = %s AND user_login LIKE 'VT2014%' AND user_email NOT LIKE 'registered%' 
                    LIMIT 1 
                    ",
                    time(), time(),$wpdb->insert_id
            )
    );
}
add_action('user_register', 'save_user_fields'); //When adding new users

I want them to be changed AFTER they have been saved and after an email-confrimation mail has been sent to actual type-in-mail by user.

With above code Wordpress says that there are already an email adress in the database and stops there... ...and that is actuall what you want in most cases

I've also tried this:

function changeusername($user_id) {
    global $wpdb;
    $wpdb->query( 
            $wpdb->prepare( 
                    "
                    UPDATE $wpdb->users 
                    SET user_email=CONCAT('registered-%s',user_email),user_login=CONCAT('registered-%s',user_login), 
                    WHERE ID = %s AND user_login LIKE 'VT2014%' AND user_email NOT LIKE 'registered%' 
                    LIMIT 1 
                    ",
                    time(), time(),$wpdb->insert_id
            )
    );
}
    add_action('register_post', 'changeusername'); //Just before saving new user to database

I want new users be changed AFTER they have been saved and after an email-confrimation mail has been sent to actual typed-in-mail by user. How could I solve that?

Share Improve this question asked Dec 2, 2013 at 11:28 bestprogrammerintheworldbestprogrammerintheworld 1,3212 gold badges19 silver badges40 bronze badges 6
  • Can you share the real problem you are trying to solve with this? Maybe there is another way to achieve your goal? – birgire Commented Dec 2, 2013 at 11:36
  • Basically - I have a registration form where users must be able to registrer themself with same email once or twice (or more). I've tried plugins like Allow Multiple Emails and it did not work (I'm using WP 3.7). – bestprogrammerintheworld Commented Dec 2, 2013 at 11:43
  • 1 why do users have to register themselves multiple times? What is the purpose of multiple accounts? – Milo Commented Dec 2, 2013 at 14:06
  • @Milo - It's a memberregistration-system for dance-lessons for children. The parents registers their children and the parents uses their own email-adress because the children doesn't have any (they could like 4-5 years of age). the best solution would be to manage this at once in on submit, BUT the parents don't use the form like that: They register the first child, come back one hour later and then register next child... ..if anyone has any suggestion how I could solve that - please tell me! :-) (login is NOT an option, that would make the whole registering harder for the users) – bestprogrammerintheworld Commented Dec 2, 2013 at 19:39
  • make the children a private custom post type associated to a single parent account. – Milo Commented Dec 2, 2013 at 19:45
 |  Show 1 more comment

1 Answer 1

Reset to default 0

You can try this skeleton plugin:

/** 
 * Plugin Name: Allow duplicate emails on registration 
 * Plugin URI: http://wordpress.stackexchange/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 ;-)

发布评论

评论列表(0)

  1. 暂无评论