5

In the BuddyPress, I want to allow register and log in using only one email domain. For instance, xxx@myemaildomain.com restore all will be disallowed.

I have checked in the BuddyPress source and found that BuddyPress is using bp_core_validate_user_signup( $user_name, $user_email ) for sign up that has a filter

return apply_filters( 'bp_core_validate_user_signup', $result );

So I have tried to use the filter to modify the user_email filed as in the code below. But it is not working.

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?

  • So given a string with an email in it, how do you test if the email belongs to a particular domain? Or do you know that part but are asking how to use the bp_core_validate_user_signup filter? – Tom J Nowell Apr 7 at 11:21
  • @TomJNowell well, I don't know if I am using this filter in wrong way. However, I found sign up the action and that solved my problem. I will post the answer later today. – pixelngrain Apr 7 at 14:14
  • The WordPress core way would be to return a WP_Error if the email domain didn't match, not just clear the address, but I don't know if BuddyPress would cope with that here. – Rup Apr 7 at 14:17
5

Finally, I found the solution. Here is the code if anyone has the same issue and want to refer it.

Using Action

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

I also found a solution using the same filter as in my question. The answer provided by @Damocles and here is the original answer link. However, for convenience, I am pasting code here.

Using Filter

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');
| improve this answer | |
  • If this works you should accept your own answer so future users know immediately that there's a working solution. :-) – Tony Djukic Apr 19 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.