Skip to content

Instantly share code, notes, and snippets.

@elhardoum
Created August 18, 2016 19:36
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 elhardoum/642bcdae4ab3b2fa5cb7963ca3b4490f to your computer and use it in GitHub Desktop.
Save elhardoum/642bcdae4ab3b2fa5cb7963ca3b4490f to your computer and use it in GitHub Desktop.
<?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