在WooCommerce上限制域名注册 [英] Limit Domain Registration on WooCommerce
本文介绍了在WooCommerce上限制域名注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何限制WooCommerce注册中对用户电子邮件的特定域的访问?
How to limit access to specific domains on WooCommerce registration for user email?
我找到了可以做到这一点的代码片段,但是由于某种原因,它在WooCommerce注册表单上不起作用.
I found this snippet of code that can do that, but it doesn't work on the WooCommerce Registration form for some reason.
但是如果我转到WP登录页面,它就可以工作.
It works if I go to the WP-login page though.
感谢您的帮助.
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
推荐答案
在WooCommerce中使用的正确钩子是woocommerce_register_post
操作钩子.尝试以下方法:
The correct hook to be used in WooCommerce is woocommerce_register_post
action hook. Try this instead:
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文件中.
经过测试,可以正常工作.
Tested and works.
这篇关于在WooCommerce上限制域名注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文