php - 阻止在 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 );
}
}
解决方案
我可以看到您的部分代码取自以下答案:
而不是使用woocommerce_register_post
你应该试试这个钩子:woocommerce_register_form_start
看看这是否可行。
推荐阅读
- reactjs - CORS:通过 webpack-dev-server 将 localhost 代理到外部 API
- java - 永久保存用户输入以供以后使用和扩展您的程序
- python - 删除 venv 会删除已安装的模块吗?
- python - 生成相关随机样本
- visual-studio-2019 - VB.Net Visual Studio 2019 社区版中的调试设置
- python - 你可以使用 C# 来编排(持久函数)一个 Python Azure 函数吗?
- python - Python:如何将另一个 df 列的值附加到当前 df col 名称
- javascript - 在 React 组件中使用分页 API
- time-series - 通过从 imputeTS 函数中提取数据来计算时间序列中的平均间隙大小
- java - 如何在 Android 的 Java 类中创建一个事件并在我的 MainActivity 中收听它?