0

I am trying to override the email_exists function to modify the way it works slightly. It is not listed as a pluggable function so I am unsure how to go about it.

I would like thef following to happen

  • Somebody tries to register with an existing email address
  • Wordpress detects that this is a duplicate
  • Wordpress automatically assigns it a random email address instead

Can something be done with filters?

UPDATE - --- I now have this in my functions file...

add_filter(
    'pre_user_email',
    'wpse163079_pre_user_email_example'
);
function wpse163079_pre_user_email_example(
    $user_email
) {
    if ( email_exists( $user_email ) ) {
        $user_email = 'randomemail3452345@example.com';
        return $user_email;
    } else {
        return $user_email;
    }
}

But this is not working, it still gives me the message that 'An account is already registered with your email address. Please login'

  • 2
    If you add what modification you had in mind, then it would be easier to help you, maybe there is another way to do what you want. – Nicolai Sep 30 '14 at 21:20
  • Good point, I have updated the original post with some more information on what I am trying to achieve – fightstarr20 Sep 30 '14 at 21:46
  • Ok, this is a woocommerce error message, as you should be very well aware, third party plug-in questions are off topic. – Nicolai Sep 30 '14 at 23:08
  • I assumed WordPress was taking care of the registration functions, looks like this isn't the case – fightstarr20 Sep 30 '14 at 23:16
1

Note: The reason this doesn't work for the OP is the use of woocommerce, otherwise, so in regards of core WordPress functionality, this should be working as described.


Update:

I assumed it is about emails on registration. Good thing you specified your needs. WordPress tries - several checks are performed - to add the new user with wp_insert_user - source: user.php. There you have the filter pre_user_email, which you can use for your purpose.

The exemplary usage would be like this:

add_filter(
    'pre_user_email',
    'wpse163079_pre_user_email_example'
);
function wpse163079_pre_user_email_example(
    $user_email
) {
    if ( email_exists( $user_email ) ) {
        // code that generates the random email address
        // do not forget to check if the random one does exist
        // assign random address as new $user_email then
        return $user_email;
    } else {
        return $user_email;
    }
}



You can't override email_exists() as it isn't a pluggable function. But a look into the source: user.php shows:

function email_exists( $email ) {
        if ( $user = get_user_by('email', $email) )
                return $user->ID;

       return false;
}

So email_exists() uses get_user_by() - source: pluggable.php -, which on the other hand is a pluggable function, so that one you could override. As you can see get_user_by() does look like this:

function get_user_by( $field, $value ) {
        $userdata = WP_User::get_data_by( $field, $value );

        if ( !$userdata )
                return false;

        $user = new WP_User;
        $user->init( $userdata );

        return $user;
}

So no hook - action or filter - you could use. But it shows that is mainly a wrapper for functionality of the WP_User class - source: capabilities.php. After taking a look at the get_data_by() method of the WP_User class you will see there is no hook available.

You did not write what you are trying to do. Maybe you can achieve what you want by simply overriding get_user_by(). Another possibility could be to additionally extend the WP_User class. I really can't say much more with the given information, but this should be a good starting point for you.

  • Not working for me, I have updated the OP again with some more information – fightstarr20 Sep 30 '14 at 22:42

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.