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

时间:2020-04-07 作者:pixelngrain

在BuddyPress中,我希望只允许使用一个电子邮件域进行注册和登录。例如,xxx@myemaildomain.com不允许全部还原。

我已经检查了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:

如何验证电子邮件,使其仅允许从一个特定的电子邮件域注册和登录?

1 个回复
最合适的回答,由SO网友:pixelngrain 整理而成

最后,我找到了解决方案。如果有人有相同的问题并希望参考,请参阅下面的代码。

使用操作

function wf_validate_email_domain()
{
    global $bp;

    $email          = $bp->signup->email;
    $email_exploded = explode(\'@\', $email);
    // split on @ and return last value of array (the domain)
    $domain = array_pop($email_exploded);

    $email_domains = get_option(\'allowed_email_domains\');

    $allowed_email_domains = array_map(\'trim\', explode(\',\', $email_domains));

    if ( ! in_array($domain, $allowed_email_domains)) {
        $bp->signup->errors[ \'signup_email\' ] = __(\'You cannot register using this email\', \'workfront\');
    }

}

add_action(\'bp_signup_validate\', \'wf_validate_email_domain\');
我还找到了一个使用与我的问题相同的过滤器的解决方案。答案由@Damocles 这是original answer link. 然而,为了方便起见,我在这里粘贴代码。

使用筛选器

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));

        $email_domains = get_option(\'allowed_email_domains\');

        $allowed_email_domains = array_map(\'trim\', explode(\',\', $email_domains));

        if ( ! in_array($domain, $allowed_email_domains)) {
            $result[ \'errors\' ]->add(\'user_email\', __(\'You cannot register using this email\', \'workfront\'));
        }

    }

    return $result;

}

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

相关推荐

作者博客页面上的BuddyPress个人资料链接

我想在作者页面上添加buddypress简介的链接,该页面显示每个作者的所有帖子。该链接需要指向buddypress个人档案,因此需要调用author meta并用bp\\u core\\u get\\u user\\u domain替换普通的author链接,以获取buddypress个人档案。我在作者页面的顶部有一个描述框,其中有指向他们的社交媒体帐户的链接(见下文),我想添加这个新链接。任何帮助都将不胜感激。<p class=\"desc\"><?php echo esc_html