Skip to content

Instantly share code, notes, and snippets.

@mattneal-stafflink
Created June 2, 2021 23:24
Show Gist options
  • Save mattneal-stafflink/624a7ee0764f62203d5051526f865bd4 to your computer and use it in GitHub Desktop.
Save mattneal-stafflink/624a7ee0764f62203d5051526f865bd4 to your computer and use it in GitHub Desktop.
Check if the submitted email on the register process is valid. Runs right before registering a user.
<?php
/**
* Check to ensure the users' email address matches the allowed list.
* Update the $valid_email_domains array to a comma separated list of string email addresses.
*
* @since 1.0.0
*/
function is_valid_email_domain($login, $email, $errors ) {
$valid_email_domains = array("imageproperty.com.au","stafflink.com.au");// whitelist email domain lists
$valid = false;
foreach( $valid_email_domains as $d ) {
$d_length = strlen( $d );
$current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
if( $current_email_domain == strtolower($d) ) {
$valid = true;
break;
}
}
// if invalid, return error message
if( $valid === false ) {
$errors->add('domain_whitelist_error',__( '<strong>ERROR</strong>: Sorry, your email address is not on the approved list. You must use a valid Image Property email address.' ));
}
}
add_action('register_post', 'is_valid_email_domain', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment