限制WooCommerce上的域名注册

时间:2018-02-27 16:31:45

标签: php wordpress woocommerce registration tld

如何在用户电子邮件的WooCommerce注册中限制对特定域的访问?

我发现这段代码可以做到这一点,但由于某些原因它不适用于WooCommerce Registration表单。

如果我去WP-login页面,它会起作用。

感谢任何帮助。

function is_valid_email_domain($login, $email, $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 === 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 ); //this works
// add_action('woocommerce_register_form', 'is_valid_email_domain',10,0); //getting errors
// add_action('user_register', 'is_valid_email_domain',10,3 ); //getting errors

1 个答案:

答案 0 :(得分:1)

在WooCommerce中使用的正确钩子是woocommerce_register_post动作钩子。试试这个:

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 );
    }
}

代码放在活动子主题(或主题)的function.php文件中。

经过测试和工作。