Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ideadude/35dfe909d80b9926e8659973fe970c64 to your computer and use it in GitHub Desktop.

Select an option

Save ideadude/35dfe909d80b9926e8659973fe970c64 to your computer and use it in GitHub Desktop.
Restrict Membership Signup by Email Domain (Useful for Education, Corporate, or Association Memberships)
<?php
/**
* Restrict Membership Signup by Email Domain
* Make sure to edit the $valid_domains array defined further below
* to include only the domains you'd like to allow.
*
* Add this code to a custom plugin. More info: https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_registration_checks_restrict_email_addresses( $value ) {
$email = $_REQUEST['bemail'];
if( ! my_checkForValidDomain( $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','my_pmpro_registration_checks_restrict_email_addresses', 10, 1 );
//Taken from: http://www.bitrepository.com/how-to-extract-domain-name-from-an-e-mail-address-string.html
function my_getDomainFromEmail( $email ) {
// Get the data after the @ sign
$domain = substr(strrchr($email, "@"), 1);
return $domain;
}
function my_checkForValidDomain( $email ) {
$domain = my_getDomainFromEmail( $email );
// Update this array to include the domains you want to allow
$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] == "*" && count( $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;
}
@laurenhagan0306

Copy link
Copy Markdown

This recipe is included in the blog post on "Restrict Membership Signup by Email Domain (Useful for Education, Corporate, or Association Memberships)" at Paid Memberships Pro here: https://www.paidmembershipspro.com/restrict-membership-signup-by-email-domain-useful-for-education-corporate-or-association-memberships/

@nicolaboscolo

nicolaboscolo commented Sep 22, 2021

Copy link
Copy Markdown

Hi @laurenhagan0306 @ideadude. Thank You for the code. It works perfectly for new users, but I get an error of a not valid domain on membership renewal (even if the user's domain is in the list). What do You think could be the issue?

@nicolaboscolo

Copy link
Copy Markdown

@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.

@hbcondo

hbcondo commented Jan 27, 2022

Copy link
Copy Markdown

Thank you for this gist and the blog post on this topic.
@nicolaboscolo, are you still experiencing an issue with renewals using this gist?
Can @ideadude confirm?

@nicolaboscolo

Copy link
Copy Markdown

@hbcondo it resolves the issue. However, I cannot guarantee the security of this modification, as I am not a PHP expert.

@hbcondo

hbcondo commented Feb 16, 2022

Copy link
Copy Markdown

thank you for your reply @nicolaboscolo. since @ideadude hasn't responded yet, would you mind posting your updated code as a comment here that resolves the error during membership renewal?

@nicolaboscolo

nicolaboscolo commented Feb 17, 2022

Copy link
Copy Markdown

@hbcondo it would be like this:
My addition is on line 11.

<?php
/**
 * Restrict Membership Signup by Email Domain
 * Make sure to edit the $valid_domains array defined further below
 * to include only the domains you'd like to allow.
 *
 * Add this code to a custom plugin. More info: https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
 */
function my_pmpro_registration_checks_restrict_email_addresses( $value ) {
	$email = $_REQUEST['bemail'];
	
	if(is_null($email)){
		return $value;
	}
	
	if( ! my_checkForValidDomain( $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','my_pmpro_registration_checks_restrict_email_addresses', 10, 1 );

//Taken from: http://www.bitrepository.com/how-to-extract-domain-name-from-an-e-mail-address-string.html
function my_getDomainFromEmail( $email ) {
    // Get the data after the @ sign
    $domain = substr(strrchr($email, "@"), 1);

    return $domain;
}

function my_checkForValidDomain( $email ) {
	$domain = my_getDomainFromEmail( $email );
	
	// Update this array to include the domains you want to allow
	$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;
}

@hbcondo

hbcondo commented Feb 17, 2022

Copy link
Copy Markdown

Thanks, @nicolaboscolo! This clarifies the issue in that we are assuming $value contains the allowed email address that the member registered with so we will skip the validation process since it was performed during initial registration. Now I realize this gist needs to be expanded for my use case to perform the following:

  • Validate TLD .edu email addresses for any education domain
  • Apply to only a specific membership level; this gist as-is applies to all membership levels
  • Line 41 in the original gist should be corrected to sizeof( $domain_to_check ) > 2

@elias1435

Copy link
Copy Markdown

I have used this but showing critical error in line number 41.

if($components[0] == "*" && sizeof( $domain_to_check > 2 ) )

would you pls check this...
thanks in advance

@elias1435

elias1435 commented Jul 6, 2022

Copy link
Copy Markdown

i have found a solution to fix the error

function my_pmpro_registration_checks_restrict_email_addresses( $value ) {
$email = $_REQUEST['bemail'];
if(is_null($email)){
return $value;
}
if( ! my_checkForValidDomain( $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','my_pmpro_registration_checks_restrict_email_addresses', 10, 1 );

//Taken from: http://www.bitrepository.com/how-to-extract-domain-name-from-an-e-mail-address-string.html
function my_getDomainFromEmail( $email ) {
// Get the data after the @ sign
$domain = substr(strrchr($email, "@"), 1);
return $domain;
}
function my_checkForValidDomain( $email ) {
$domain = my_getDomainFromEmail( $email );
// Update this array to include the domains you want to allow
$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(!empty($components[0]->config == "*") && sizeof($domain_to_check->config > 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

ideadude commented Apr 1, 2025

Copy link
Copy Markdown
Author

I've just now fixed the line 41 error. Thanks.

@ideadude

ideadude commented Apr 1, 2025

Copy link
Copy Markdown
Author

@ipokkel

ipokkel commented Apr 2, 2025

Copy link
Copy Markdown

Another updated version that allows applying the email domain restrictions to all or only specific memberships levels - https://gist.github.com/ipokkel/e939a58b9b45bf4e1747a2b7ef518254

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