Skip to main content
changed answer based on some additional information
Source Link
butlerblog
  • 5.1k
  • 3
  • 26
  • 42

The email_exists() function containsBased on your code, I think the email_exists filterproblem may have been that you could use for thiswere checking a variable that was undefined. It receives the You were checking $user_id$db_email and, which in your function was an undefined value, so you'd be looking for an empty result. You need to check the value passed to $emailemail_exists(). It will expect you to return either the user's ID (ifwhich would be the email existsbeing validated in the form).

If the patterns applied in your pluggable get_user_by() function result in the equivalent of how you are obfuscating the emails, orthen you should get a false booleancorrect match if it does not:the email exists.

add_filterfunction email_exists( 'email_exists',$email 'my_custom_email_exists',) 10,{
 2 );
function my_custom_email_exists( $user_id// You had this as checking $db_email, $emailwhich )would {
have been
    /*
/ undefined. It should be *checking dofor yourthe thingvalue here..passed to
    // this function - $email.
    $user *= justget_user_by( make'email', sure$email to);
 return the $user_id if the( user$user exists) {
     * or a false$user_id boolean= if$user->ID;
 they don't.  } else {
     */   $user_id = false;
    }
    return $user_id;apply_filters( 'email_exists', $user_id, $email );
}

The email_exists() function contains the email_exists filter that you could use for this. It receives the $user_id and the $email. It will expect you to return either the user's ID (if the email exists), or a false boolean if it does not:

add_filter( 'email_exists', 'my_custom_email_exists', 10, 2 );
function my_custom_email_exists( $user_id, $email ) {

    /*
     * do your thing here...
     * just make sure to return the $user_id if the user exists
     * or a false boolean if they don't.
     */
    return $user_id;
}

Based on your code, I think the problem may have been that you were checking a variable that was undefined. You were checking $db_email, which in your function was an undefined value, so you'd be looking for an empty result. You need to check the value passed to email_exists() (which would be the email being validated in the form).

If the patterns applied in your pluggable get_user_by() function result in the equivalent of how you are obfuscating the emails, then you should get a correct match if the email exists.

function email_exists( $email ) {
    // You had this as checking $db_email, which would have been
    // undefined. It should be checking for the value passed to
    // this function - $email.
    $user = get_user_by( 'email', $email );
    if ( $user ) {
        $user_id = $user->ID;
    } else {
        $user_id = false;
    }
    return apply_filters( 'email_exists', $user_id, $email );
}
Source Link
butlerblog
  • 5.1k
  • 3
  • 26
  • 42

The email_exists() function contains the email_exists filter that you could use for this. It receives the $user_id and the $email. It will expect you to return either the user's ID (if the email exists), or a false boolean if it does not:

add_filter( 'email_exists', 'my_custom_email_exists', 10, 2 );
function my_custom_email_exists( $user_id, $email ) {

    /*
     * do your thing here...
     * just make sure to return the $user_id if the user exists
     * or a false boolean if they don't.
     */
    return $user_id;
}