Seacrist
Seacrist

Reputation: 1

How to whitelist specific email domains on signup

We have a publishing site on WordPress that we want anyone to be able to sign up for but people who sign up with certain email domains will have access to more content. We're using the Paid Memberships Pro WordPress plugin and trying to figure out how to assign them membership when they register but we don't know php.

The reason why we can't just make a free membership level for them to choose when they sign up is because we want them to sign up to our site through the Auth0 login widget which doesn't allow them to choose membership levels.

We thought an array would be useful, something like:

$domain_name = array("ipqpubs.com", "ipqpubs.org");

if (in_array("ipqpubs.com", $domain_name))

or

if (domain_list($domains))
    $hasaccess = true;

But we don't know if that would work or where to go from there. We found some useful looking stuff in their files like:

function pmpro_hasMembershipLevel( $levels = null, $user_id = null ) {
    global $current_user, $wpdb;

    $return = false;

    if ( empty( $user_id ) ) {
        $user_id = $current_user->ID;
    }

if ( ! empty( $user_id ) && is_numeric( $user_id ) ) { // get membership 
levels for given user
        $membership_levels = pmpro_getMembershipLevelsForUser( $user_id );
    } else {
        $membership_levels = null; // non-users don't have levels
    }

pmpro_getMembershipLevelsForUser( $user_id );
return pmpro_changeMembershipLevel( 0, $user_id, $old_level_status );
}           

So we thought maybe one of those or something similar could be used to assign them a membership level as soon as they sign up so they don't need to do anything else and can see everything on our site.

Thanks in advance for your help

Upvotes: 0

Views: 238

Answers (1)

Maximilian
Maximilian

Reputation: 531

If you can get the users email, you can try to match it against a regular expression containing the following:

$userEmail = $currentUser->email ?? ""; // Assumed property of $currentUser
if (preg_match('/(@ipqpubs.com|@ipqpubs.org)/', $userEmail)) {
    // User has an email that matches one of the domains!
} else {
    // User does not have an email that matches any of the domains.
}

If the list of "approved" domains gets long, you can do as follows:

$approvedDomains = ['ipqpubs.com', 'ipqpubs.org'];
$approvedDomainFilter = '/('.join('|@', $approvedDomains).')/';

$userEmail = $currentUser->email ?? ""; // Assumed property of $currentUser
if (preg_match($approvedDomainFilter, $userEmail)) {
    // User has an email that matches one of the domains!
} else {
    // User does not have an email that matches any of the domains.
}

Do not use this solution as-is. You need to make sure that the $userEmail variable gets set to the user's actual email and write the code that should be executed if the email matches.

Upvotes: 1

Related Questions