MemberPress: Only Allow Sign-Ups From Certain Email Domains
Rejects MemberPress registrations unless the email address belongs to one of your allowed domains, ideal for company or school memberships.
Opens WPIDE on
Code
if ( ! class_exists( 'MeprUser' ) ) {
return;
}
$wpide_mepr_allowed_domains = array( 'example.com', 'example.edu' );
add_filter( 'mepr-validate-signup', function ( $errors ) use ( $wpide_mepr_allowed_domains ) {
if ( is_user_logged_in() || empty( $_POST['user_email'] ) ) {
return $errors;
}
$email = sanitize_email( wp_unslash( $_POST['user_email'] ) );
$domain = strtolower( substr( strrchr( $email, '@' ), 1 ) );
foreach ( $wpide_mepr_allowed_domains as $allowed ) {
if ( $domain === $allowed || substr( $domain, -strlen( '.' . $allowed ) ) === '.' . $allowed ) {
return $errors;
}
}
$errors[] = sprintf( 'Please sign up with your %s email address.', implode( ' or ', $wpide_mepr_allowed_domains ) );
return $errors;
} );
Notes
Existing members logging in are not affected. Subdomains count, so "example.edu" also allows "mail.example.edu".
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.