Black listing email domains from creating accounts

This topic has 4 replies, 3 voices, and was last updated 9 months, 2 weeks ago ago by Tony Rodriguez

  • Avatar: dominus
    dominus
    Participant
    July 19, 2023 at 16:02

    Good day,

    I have tried the following approaches to block spammers from creating further accounts but unfortunately nether of the 2 approaches worked.

    Users are still able to create accounts as per normal so I believe either you have custom actions or shortcodes that I am missing.

    Can you please assist me, the code I tried is below.

    add_action('woocommerce_register_post','is_valid_registration_email_domain', 10, 3 );
    function is_valid_registration_email_domain( $username, $email, $validation_errors ){
        $valid_email_domains = array( 'gmail.com', 'yahoo.com' ); // Allowed domains
        $valid = false; // sets default validation to false
        foreach( $valid_email_domains as $d ){
            $d_length = strlen( $d );
            $current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
            if( $current_email_domain != strtolower($d) ){
                $valid = true;
                break;
            }
        }
    
        // Return error message for invalid domains
        if( ! $valid ){
            $error_text = __( "<strong>ERROR</strong>: Registration is only allowed from selected approved domains. If you think you are seeing this in error, please contact the system administrator.", "woocommerce" );
            $validation_errors->add( 'domain_whitelist_error', $error_text );
        }
    }

    or

    // Allow Registration Only from @warrenchandler.com email addresses
    
    function is_valid_email_domain($login, $email, $errors ){
     $valid_email_domains = array("warrenchandler.com","warrenchandler.co.uk");// allowed domains
     $valid = false; // sets default validation to false
     foreach( $valid_email_domains as $d ){
      $d_length = strlen( $d );
      $current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
     if( $current_email_domain != strtolower($d) ){
      $valid = true;
      break;
     }
     }
     // Return error message for invalid domains
     if( $valid === false ){
    
    $errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: Registration is only allowed from selected approved domains. If you think you are seeing this in error, please contact the system administrator.' ));
     }
    }
    add_action('register_post', 'is_valid_email_domain',10,3 );
    3 Answers
    Avatar: Rose Tyler
    Rose Tyler
    Support staff
    July 20, 2023 at 16:50

    Hello, Dominus,

    Thank you for contacting us and for using XStore.

    Please note that additional customization in files is out of our basic support scope.

    We have developed the following functions for you, simply add them to the functions.php of the child theme.
    Note: if you are using additional plugins for user registration, the sent functions may not work.

    if (! function_exists('et_validate_registration_by_email_domain')){
        // Hook into the registration_errors action (To prevent default WordPress registration)
        add_action('registration_errors', 'et_validate_registration_by_email_domain', 10, 3);
    
        function et_validate_registration_by_email_domain($errors, $sanitized_user_login, $user_email) {
            // Define the list of allowed email domains
            $allowed_domains = array('gmail.com', 'yahoo.com');
    
            // Extract the domain part of the email address
            $user_email_parts = explode('@', $user_email);
            $user_domain = strtolower(end($user_email_parts));
    
            // Check if the user's email domain is in the allowed list
            if (!in_array($user_domain, $allowed_domains)) {
                $errors->add('email_domain_not_allowed', __('Registration is allowed only with specific email domains.', 'xstore-child'));
            }
    
            // Return the modified errors object
            return $errors;
        }
    }
    
    if (! function_exists('et_wc_validate_registration_by_email_domain')){
        // Hook into the woocommerce_new_customer_data filter (To prevent WooCommerce registration)
        add_filter('woocommerce_new_customer_data', 'et_wc_validate_registration_by_email_domain', 10, 1);
    
        function et_wc_validate_registration_by_email_domain($customer_data) {
            // Define the list of allowed email domains
            $allowed_domains = array('gmail.com', 'yahoo.com');
    
            // Get the email address from the customer data
            $user_email = $customer_data['user_email'];
    
            // Extract the domain part of the email address
            $user_email_parts = explode('@', $user_email);
            $user_domain = strtolower(end($user_email_parts));
    
            // Check if the user's email domain is in the allowed list
            if (!in_array($user_domain, $allowed_domains)) {
                throw new Exception(__('Registration is allowed only with specific email domains.', 'xstore-child'));
            }
    
            // Return the modified customer data
            return $customer_data;
        }
    }

    Kind Regards,
    8theme team

    Avatar: dominus
    dominus
    Participant
    July 21, 2023 at 05:47

    Thank you so much, this works perfectly!

    Avatar: Tony Rodriguez
    Tony Rodriguez
    Support staff
    July 21, 2023 at 07:37

    Hello, @dominus,

    We are delighted to have you with us. If you are willing, we would greatly appreciate it if you could leave a five-star rating for our theme and customer and technical support on ThemeForest at the following link: http://themeforest.net/downloads. Doing so would enable us to provide more updates and dedicated support in the future. Your support would be greatly appreciated.

    Thank you for reaching out to us. We hope you have a wonderful day.

    Sincerely,
    8Theme’s Team

  • Viewing 4 results - 1 through 4 (of 4 total)

The issue related to '‘Black listing email domains from creating accounts’' has been successfully resolved, and the topic is now closed for further responses

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.