WordPress:BuddyPress:只允许一个电子邮件域注册

WordPress: BuddyPress: Allow only one email domain to register

在 BuddyPress 中,我想允许仅使用一个电子邮件域注册和登录。例如,xxx@myemaildomain.com restore all 将被禁止。

我查看了 BuddyPress 源代码,发现 BuddyPress 正在使用 bp_core_validate_user_signup( $user_name, $user_email ) 进行注册,该注册有一个过滤器

return apply_filters( 'bp_core_validate_user_signup', $result );

所以我尝试使用过滤器修改 user_email 文件,如下面的代码所示。但它不起作用。

function wf_validate_email_domain($result)
{

    $email = $result[ 'user_email' ];

    // make sure we've got a valid email
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // split on @ and return last value of array (the domain)
        $domain = array_pop(explode('@', $email));

        if ($domain != 'mydomain.com') {
            $result[ 'user_email' ] = '';
        }
    }

    return $result;

}

add_filter('bp_core_validate_user_signup', 'wf_validate_email_domain', 9999);

Question:

How can I validate email so it allows to register and login only from one specific email domain?

根据文档,$result 应该包含一个 errors 字段: https://www.buddyboss.com/resources/reference/functions/bp_core_validate_user_signup/

因此,您应该添加一个错误,而不是像这样将邮件地址设置为空字符串:

function wf_validate_email_domain($result)
{
    $allowed_domain = 'apolloblake.com';
    $email = $result[ 'user_email' ];

    // make sure we've got a valid email
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // split on @ and return last value of array (the domain)
        $domain = array_pop(explode('@', $email));

        if ($domain != $allowed_domain) {
            $result[ 'errors' ]->add( 'user_email', 
            "You may only register with mail addresses on @${allowed_domain}." );
        }
    }

    return $result;

}

您可能需要将此消息设为多语言,但这取决于您的 WP 设置。