Home / Archive / MemberPress: Restrict Email Domains on Registration
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Restrict Email Domains on Registration

This code snippet will limit the membership registration to only the allowed list of email domains. Thus, users will be able to register for memberships only with email addresses that match allowed email domains.

The sample code snippet will allow registration only to users with gmail.com and hotmail.com email addresses.

The code snippet will display the “Your email domain is blocked from signing up.” error message if registration is attempted with the email address that doesn’t match the allowed email domains list.

The code snippet should be updated by replacing the current list of domains ('gmail.com', 'hotmail.com') with the actual list of allowed email domains in this line:

$allowed_email_domains = array('gmail.com', 'hotmail.com');

To edit the error message, modify the "Your email domain is blocked from signing up." text in this line:

$errors[] = 'Your email domain is blocked from signing up.';

Code Preview
php
<?php
function mepr_restrict_email_domains($errors) {
  # Allowed email domains.
  $allowed_email_domains = array('gmail.com', 'hotmail.com');
  $email_address = isset($_POST['user_email']) ? sanitize_email(trim($_POST['user_email'])) : '';
  # Don't bother validating if the email is empty.
  if(empty($email_address)) {
    return $errors;
  }
  $user_email_array = explode('@', $email_address);
  $user_email_domain = array_pop($user_email_array);
  # Check if the user's email domain is allowed.
  if (!in_array($user_email_domain, $allowed_email_domains)) {
    $errors[] = 'Your email domain is blocked from signing up.';
    return $errors;
  }
  return $errors;
}
add_filter('mepr-validate-signup', 'mepr_restrict_email_domains');

Comments

Add a Comment