Skip to content

Instantly share code, notes, and snippets.

@denisbaranov
Last active February 2, 2021 16:56
Show Gist options
  • Save denisbaranov/864f69ef44b96fcd93b105c52dcefad9 to your computer and use it in GitHub Desktop.
Save denisbaranov/864f69ef44b96fcd93b105c52dcefad9 to your computer and use it in GitHub Desktop.
The following code requires users to register with a business email only and blocks popular email services, such as @gmail.com, @yahoo.com. ect. The code below requires a user email to be collected during registration.
<?php
/**
* This example shows how to validate the email address by domain on registration.
* The code below requires a user email to be collected during registration.
*
* Change the array $forbidden_email_domains - add or remove domains you need.
* Add this code snippet to the end of the file functions.php in the active theme directory.
*
* Ultimate Member documentation: https://docs.ultimatemember.com/
* Ultimate Member support (for customers): https://ultimatemember.com/support/ticket/
*/
/**
* Custom validation for the field "E-mail Address" by domain on registration.
* @author Ultimate Member support <support@ultimatemember.com>
* @param array $args
*/
function um_validate_email_domain( $args ) {
// Add forbidden email domains here
$forbidden_email_domains = apply_filters( 'um_forbidden_email_domains', array(
'gmail.com',
'yahoo.com',
'hotmail.com'
) );
// Change error message here
$message = __( 'You can not use this email domain for registration. Please use a business email address.', 'ultimate-member' );
if ( isset( $args['user_email'] ) && is_email( $args['user_email'] ) ) {
$email_domain = array_pop( explode( '@', trim( $args['user_email'] ) ) );
if ( in_array( $email_domain, $forbidden_email_domains ) ) {
UM()->form()->add_error( 'user_email', $message );
}
}
}
add_action( 'um_submit_form_errors_hook__registration', 'um_validate_email_domain', 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment