Limit BuddyPress registration by domain

I am currently running a few WordPress installations with the great BuddyPress plugin. If you don’t know this plugin yet: you can create your own social network using this plugin. For example, it is possible to add groups, forums, membership, and even “@” commenting and discussions (like Twitter). Pretty nifty, really.

One thing that stumped me for a while was how to limit registrations to a particular domain. In my case, I wanted to limit registration to users with EDU email addresses (because the network is affiliated with our Five College System).

As it turns out, the code is not too involved. I just had to disassemble a discontinued plugin to find it. In any case… it works well and can even be expanded to use a blacklist/whitelist approach. Below is the code that you need to put into your functions.php file in your template.

I am using this on a single-site installation with the default BuddyPress template, and the code below only checks for EDU, so your mileage might vary. Adapt the code as you see fit.

// Limit signup to EDU emails - could also be used for banning domains

// Function that checks and returns error
function bp_as_restrict_signup_domains( $result ) {

	// Send error if this test fails
	if ( bp_as_check_email_domains( $result['user_email'] ) ) {
		$result['errors']->add('user_email', __('You must use an EDU email to sign up!', 'bp-restrict-email-domains' ) );
	};
	return $result;

};

add_filter( 'bp_core_validate_user_signup', 'bp_as_restrict_signup_domains' );

// Helper function for the actual test
function bp_as_check_email_domains( $user_email ) {

	$email_domain = strtolower( substr( $user_email, 1 + strpos( $user_email, '@' ) ) );

	// Could check against array here too
	$is_edu = strpos( $email_domain , "edu" );

	if ($is_edu === false) {
		// Error will be raised
		return true;
	}
	else {
		return false;
	};

};

I just implemented a banning routine that is actually a bit cleaner than the one above. Here’s the code:

// ====================================
// Limit signup by banning domains
function bp_as_restrict_signup_domains( $result ) {

 $banned = array(
 'spam1.com', 
 'spam2.com'
 );
 $error = 'Your email domain has been the source of spam. Please use another email address.';

 $email = $result['user_email']; 
 $domain = array_pop(explode('@', $email));
if ( in_array($domain, $banned))
 {
 $result['errors']->add('user_email', __($error, 'bp-restrict-email-domains' ) );
 };
 return $result;
}
add_filter( 'bp_core_validate_user_signup', 'bp_as_restrict_signup_domains' );

Source:

  • Bis

    Thanks a lot dude – this really helped me – I was using another email whitelisting script which was hooking into the action “registration_errors” but that was not working – this one works just fine! :-)

  • Simon

    Thank you Alex,

    I used the first snippet and it works perfectly. I am now wondering how to limit registration to several domains, e.g. example.com, alternative.com, other.com. I guess this is very easy, but I am not a php expert…

    Any help would be much appreciated!

    • Just use the second snippet and permit instead of prohibit.

      • Simon

        Thanks Alexander, logical indeed.

        • fer

          Thanks Alexander. I’m using a whitelist/blacklist plugin, but it doesn’t work propperly, got 20 spam subscribers a day.
          May i ask what a snipper is? Should i use the first or the second code?

          In my case, if i want to allow registration only to users from my university with @zhdk.ch, should i then replace every part with $is_edu with $is_zhdk.ch and replace the words prohibit with permit?

          I’m not a programmer, but i can apply changes if explained well :-P
          Kind regards, i would really appreciate your help :-)

          • Gruezi! No problem, that’s how I learned programming, too.

            You need to copy the first snippet into the functions.php file (in the template) before the ?> line. Then change the following line:

            $is_edu = strpos( $email_domain , “edu” );

            to this:

            $is_edu = strpos( $email_domain , “zhdk.ch” );

            and you are all set. Should work without problems. If you need more information, look up the PHP function strpos.

  • abbey

    thank you very much, just what i need,

    regards

  • abbey

    Hi,

    thanks for the hack, but can you please share how to use it to blacklist some domain.

    reagards

    • I don’t have a snippet handy but all you need to do is create an array with domains and then iterate through it with a simple comparison (for example using the strstr function).

    • Just posted a snippet that does just what you want…

  • Thanks! This was quite helpful.