Forked from kimwhite/my-pmpro-restrict-registration-by-emaildomain.php
Last active
October 4, 2022 23:05
-
-
Save michaelbeil/1443d3a6ab6be3d83ebd48678d4c2e34 to your computer and use it in GitHub Desktop.
Restrict Membership Level Signup by Email Domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Restrict Membership Signup by Email Domain & Level | |
* 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; | |
$protected_levels = array( | |
10 => array( 'ugent.be' ), | |
); | |
if ( ! array_key_exists( $pmpro_level->id, $protected_levels ) ) { | |
return $value; | |
} | |
$email = $_REQUEST['bemail']; | |
if ( ! my_checkForValidDomain( $email, $protected_levels[ $pmpro_level->id ] ) ) { | |
global $pmpro_msg, $pmpro_msgt; | |
$pmpro_msg = 'Only @ugent.be will be granted access'; | |
$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, $valid_domains ) { | |
$domain = my_getDomainFromEmail( $email ); | |
if ( ! is_array( $valid_domains ) ) { | |
return true; | |
} | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment