Last active
May 26, 2016 23:54
-
-
Save eighty20results/405c3b6ba442b06263e9e4f670cab170 to your computer and use it in GitHub Desktop.
Check whether the new member has an email address that matches the specified domain and the level they're trying to sign up for is a free level.
This file contains hidden or 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 | |
/* | |
Plugin Name: PMPro e-mail domain check | |
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/ | |
Description: Check if a domain is on the "allow" list for registration | |
Version: .1 | |
Author: Thomas Sjolshagen @ Stranger Studios <[email protected]> | |
Author URI: http://www.eighty20results.com/thomas-sjolshagen/ | |
*/ | |
function restrict_email($value) | |
{ | |
// skip this check if we haven't got a level yet. | |
if (!isset($_REQUEST['level'])) { | |
return $value; | |
} | |
$email = $_REQUEST['bemail']; | |
$current_level = isset($_REQUEST['level']) ? intval($_REQUEST['level']) : null; | |
$level = pmpro_getLevel($current_level); | |
if(true === pmpro_isLevelFree($level) && false === is_valid($email)) | |
{ | |
global $pmpro_msg, $pmpro_msgt; | |
//TODO: Set the error text to be whatever you want it to be: | |
$pmpro_msg = "Please enter a valid .edu address"; | |
$pmpro_msgt = "pmpro_error"; | |
$value = false; | |
} | |
return $value; | |
} | |
add_filter('pmpro_registration_checks','restrict_email', 10, 1); | |
function is_valid($email) | |
{ | |
$test_domain = getDomainFromEmail($email); | |
$valid_domains = array("*.edu"); | |
foreach($valid_domains as $v_domain) | |
{ | |
$components = explode(".", $v_domain); | |
if($components[0] == "*") | |
{ | |
unset($components[0]); | |
$v_domain = implode('.', $components); | |
} | |
if((stripos($test_domain, $v_domain) !== false)) { | |
return true; | |
} | |
} | |
// The domain specified in the email address is one of the valid ones. | |
return false; | |
} | |
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment