首页 > 解决方案 > 阻止在 WooCommerce 中注册的某些域

问题描述

我试图在我的functions.php 中创建一个函数来阻止某些域注册。我知道有一些插件可以完成同样的工作,但我只是不想安装插件来让事情变得简单。

我已经尝试了下面的功能,但它似乎无法正常工作。在此示例中,我使用了 gmail.com 和 yahoo.com,但有一些已知的域会产生大量垃圾邮件。

add_action('woocommerce_register_post','is_invalid_registration_email_domain', 10, 3 );
function is_invalid_registration_email_domain( $username, $email, $validation_errors ){
$invalid_email_domains = array( 'gmail.com', 'yahoo.com' ); // Forbidden domains
$valid = false; // sets default validation to false
foreach( $invalid_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 );
}
}

标签: phpwordpresswoocommercecustom-theme

解决方案


我可以看到您的部分代码取自以下答案:

在 WooCommerce 上限制域名注册

而不是使用woocommerce_register_post你应该试试这个钩子:woocommerce_register_form_start

看看这是否可行。


推荐阅读