Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
/**
* Plugin Name: Restrict mail TLD registration
* Plugin URI: http://samelh.com
* Description: Allow/disallow user registration for email TLD
* Author: Samuel Elh
* Author URI: http://samelh.com
* Version: 0.1
*/
function se__verify_tld( $email ) {
/* enter the TLDs to allow into following array, e.g array( "be", "nl", "cr" ) in lowercase */
$allowed_tlds = array( "be", "nl" ); // allowing Belgium and Netherlands emails
/* OR, enter few TLDs to prevent (forbidden) in lowercase */
$forbidden_tlds = array();
if ( empty( $email ) ) return false;
$tld = strtolower( substr( $email, strpos( $email, '.' )+1 ) );
if ( empty( $tld ) ) return true; // no tld caught, trigger error
else $tld = strtolower( $tld );
if ( !empty( $forbidden_tlds ) ) {
if ( in_array($tld, $forbidden_tlds) ) {
return true;
}
}
else if ( !in_array($tld, $allowed_tlds) ) {
return true;
}
return false;
}
add_filter( 'registration_errors', function( $errors, $user_login, $user_email ) {
if ( se__verify_tld( $user_email ) ) {
$errors->add( "tld_exception", "Sorry, you can not sign up with emails from this TLD" );
}
return $errors;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment