wordpress 中如何禁止特定邮箱后缀的用户注册
要在WordPress中禁止以特定邮箱后缀(例如@gmail.com)进行用户注册,您可以通过添加自定义代码到您的主题的 functions.php
文件或使用一个自定义插件来实现这一功能。下面提供了一个示例代码,用于检查用户注册时使用的邮箱后缀,并在匹配到指定后缀时阻止注册。
请按照以下步骤操作:
广告
- 访问您的WordPress网站文件:使用FTP客户端或通过您的网站托管控制面板的文件管理器访问您的WordPress站点文件。
- 编辑
functions.php
文件:找到您当前主题的functions.php
文件。这通常位于/wp-content/themes/your-theme-name/
目录下。建议使用子主题来进行更改,以避免将来更新主题时丢失自定义代码。 - 添加自定义代码:在
functions.php
文件的末尾添加以下代码:
function disable_specific_email_domain_registration( $errors, $sanitized_user_login, $user_email ) { // 设置不允许注册的邮箱后缀 $restricted_domains = array( 'gmail.com' ); // 检查用户邮箱后缀 $email_domain = substr( strrchr( $user_email, "@" ), 1 ); if ( in_array( $email_domain, $restricted_domains ) ) { // 添加自定义错误消息 $errors->add( 'email_domain_error', __( '<strong>ERROR</strong>: Registration using this email domain is not allowed.', 'textdomain' ) ); } return $errors; } add_filter( 'registration_errors', 'disable_specific_email_domain_registration', 10, 3 );
- 保存文件:保存您对
functions.php
文件所做的更改。 - 测试:尝试使用一个以
@gmail.com
结尾的邮箱地址进行注册,以确保该代码正常工作,用户不能使用这些邮箱后缀进行注册。
通过上述步骤,您就可以在WordPress网站上禁止使用 @gmail.com
后缀的邮箱地址进行用户注册了。如果需要禁止其他的邮箱后缀,只需将 $restricted_domains
数组中的值相应更改即可。