Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save strangerstudios/42399a77617bcddf2c3c to your computer and use it in GitHub Desktop.
Save strangerstudios/42399a77617bcddf2c3c to your computer and use it in GitHub Desktop.
Restrict Membership Signup by Email Domain (Useful for Education, Corporate, or Association Memberships)
<?php
function restrict_email($value)
{
$email = $_REQUEST['bemail'];
if(!check_validity($email))
{
global $pmpro_msg, $pmpro_msgt;
$pmpro_msg = "Please enter a valid email address";
$pmpro_msgt = "pmpro_error";
$value = false;
}
return value;
}
add_filter('pmpro_registration_checks','restrict_email', 10, 1);
//Taken from: http://www.bitrepository.com/how-to-extract-domain-name-from-an-e-mail-address-string.html
function getDomainFromEmail($email)
{
// Get the data after the @ sign
$domain = substr(strrchr($email, "@"), 1);
return $domain;
}
function check_validity($email)
{
$domain = getDomainFromEmail($email);
$valid_domains = array("yahoo.com", "*.gmail.com", "*.domain.uk");
foreach($valid_domains as $valid_domain)
{
$components = explode(".", $valid_domain);
$domain_to_check = explode(".", $domain);
if($components[0] == "*" && sizeof($domain_to_check > 2))
{
if($components[1] == $domain_to_check[1] && $components[2] == $domain_to_check[2])
{
return true;
}
}
else
{
if(!(strpos($valid_domain, $domain) === false))
return true;
}
}
return false;
}
@ideadude
Copy link

I've forked this gist here: https://gist.github.com/ideadude/35dfe909d80b9926e8659973fe970c64

I can no longer edit gists created under the strangerstudios user and needed to fix a few things.

@nicolaboscolo
Copy link

@laurenhagan0306 @ideadude, i propose to add:
if(is_null($email)){
return $value;
}
in line 11, because if the membership is renewed, $email is NULL. However the NULL check is done in the native pmpro filter.

@elias1435
Copy link

I have used this but showing critical error in line number 37. would you pls check this...

thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment