0

More precisely questions : How to override email_exists via get_user_by ?

I have chosen to render unusable/unreadable email adresses from database.

I added a few elements(chars) to email address just after sending email to admin and user (user registration), and then I must remove these added elements to verify if email exists or display on user account or use for mailing list.

To decrypt the email I must use substr whith the value associated to $email field of get_user_by to remove elements added before. I read that email_exists use get_user_by which is a pluggable function. But I don't know how to use the result of get_user_by inside email_exists.

Is it possible ? How should I do ?

        function get_user_by( $field, $db_email ) {
            $field = 'email';
            //function to decrypt my email from database
            $ex = preg_split('\'@\'', $nr , -1);
            $dem = substr(substr($ex[0],2),0,-2);
            $rdem = substr($dem,0,2).substr($dem,2+1,strlen($dem)-2);
            $db_email = $rdem. '@' .$ex[1];
            
            $userdata = WP_User::get_data_by( 'email', $db_email );

            if ( ! $userdata ) {
                return false;
            }

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

        return $user;
        }
        
        function email_exists( $email ) {
            $user = get_user_by( $field, $db_email );
            if ( $user ) {
                $user_id = $user->ID;
            } else {
                $user_id = false;
            }
            return apply_filters( 'email_exists', $user_id, $email );
        }
        
        if (email_exists($email)){
            lq_errors()->add("emailexists","This email address already exist");
        }

1 Answer 1

0

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 );
}
7
  • I'm not sure to understand. How I distinguish the $email variable from the value of the $email field in the database corresponding to the user id. How I apply preg_replace on $email field from database before comparing it with $email value inside Form ? In developper.wordpress get_user_by from email_exists function use the field 'email' but I can't use it to apply a function ! In your response butlerblog , does $email from my_custom_email_exists function get the field $email from database ?
    – imagIne
    Mar 19, 2021 at 9:45
  • I would have to hook get_user_by to apply the preg_replace function to email_exists. But I don't know how. How can I use or insert get_user_by inside email_exists? Or how do I force email_exists to use a custom get_user_by?
    – imagIne
    Mar 21, 2021 at 10:41
  • Actually, with the edit to your question, I better see how you are using get_user_by() as a pluggable function. That's probably better than filtering because it covers every instance of get_user_by() when it's getting the user by email. I've edited my answer above to shift gears and hopefully clarify.
    – butlerblog
    Mar 22, 2021 at 13:50
  • thanks to help me butlerblog.But Inever know which $email variable I must use to apply my substr function.For me, the $email var is the same for input $email and the value corresponding to $email field from database! That's why I would use another var $db_email.I think I don't understand whatemail_exists do ! I thought that get_user_by('email', $email) loop on email values in database, am I wrong? As I tried to explain previously I want to convert on the fly( or apply a filter to) values of the email field from database with substr. one example : in database I have e1xampleta@email.com.
    – imagIne
    Mar 22, 2021 at 20:40
  • During the process of email_exists function the email value must return example@email.com. It is this result that I want to compare to the input field filled by another user to verify if the email already exist.
    – imagIne
    Mar 22, 2021 at 20:40

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.