Restricting Registration to Certain Email Domains

Hi there. Is there a way to restrict registration on s2 Member to only certain email domains or is this a function that requires another plugin to help support that?

Thanks in advance.

This can be done with some code. Are you talking about free or paid subscribers and, if the latter, which payment processor are you using?

Hi Tim,

We will only be having free subscribers. And I’m not sure if it’s relevant, but in addition to having membership we will also be running WP courseware to offer free training to individuals.

No, that won’t make any difference, but thank you for including that information. So many people post a question but leave out something vital!

You will need the Pro version of s2member for this.

What you then need to do is to create a new file. Use a plain text program (i.e. like Notepad, not a wordprocessor) for this. I suggest calling the s2-whitelist-email-domains.php

Then paste the following into that file:

<?php
add_filter( 'ws_plugin__s2member_pro_paypal_form_submission_validation_response', 'webby_restrict_domains', 10, 3 );

add_filter('ws_plugin__s2member_pro_paypal_form_submission_validation_response', 'webby_restrict_domains', 10, 3);

function webby_restrict_domains( $response, $form, $s ) {
    $whitelist = array(
		'mydomain.com',
		'yourdomain.com',
    );
    if( !$response ) { // No other errors produced by s2Member itself?
        if( $form === 'registration' ) { // Any new registration, or any new user completing checkout.
            $domain = ''; // Initialize.
            if( isset( $s['email']) && strpos( ( string )$s['email'], '@') !== FALSE ) {
                list(, $domain ) = explode('@', ( string )$s['email'], 2);
			}

            if( !$domain || !in_array( strtolower( $domain ), array_map( 'strtolower', $whitelist ), TRUE ) ) {
                $response = array(
                    'error'    => TRUE, // This is an error.
                    'response' => _x( 'ERROR: You must register using an email address from the appropriate organization.', 's2member-front', 's2member' ),
                );
			}
        }
	}
    return $response; // Always return the filtered response.
}

Change mydomain.com and yourdomain.com to whatever email domains you need. You can have as many as you want.

Then save and upload this file to the mu-plugins folder in your WordPress installation. (If you don’t have an mu-plugins folder, you will need to create it within the wp-content folder.

This code will work if you use either the free PayPal or Stripe s2Member Pro Form. (If you will be using one, you can remove the other filter.) You will NOT need to connect up your site to either PayPal or Stripe to make this work.

1 Like