Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaryOJob/dc76cb56d69112f1eb1a60754996aabe to your computer and use it in GitHub Desktop.
Save MaryOJob/dc76cb56d69112f1eb1a60754996aabe to your computer and use it in GitHub Desktop.
Paid Memberships Pro - Restrict Email for More Than One Level
<?php // Do not copy this line
/**
* 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 ) {
global $pmpro_level;
if ( $pmpro_level->id == '1' || $pmpro_level->id == '2' ) { // Enter level IDs you would like to skip where 1 and 2 are.
return $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( 'mydomain.com' ); // where mydomain.com is your site domain address
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment