Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Last active June 30, 2022 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JarrydLong/d6456d87bdd6286883069ab222c5fe68 to your computer and use it in GitHub Desktop.
Save JarrydLong/d6456d87bdd6286883069ab222c5fe68 to your computer and use it in GitHub Desktop.
<?php //do not copy
/**
* This recipe allows you to restrict partial user email addresses during checkout.
* You will be able to specify that all @paidmembershipspro.com email addresses are allowed to sign up, without
* having to enter each user's full email address.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function mypmpro_remove_actions() {
remove_filter( "pmpro_registration_checks", "pmprorh_pmpro_registration_checks" );
}
add_action( 'init', 'mypmpro_remove_actions' );
/**
* Registration checks the restricted email domain
*/
function mypmprorh_pmpro_registration_checks( $okay ) {
global $current_user;
//only check if we're okay so far and there is an email to check
if( $okay && ( !empty($_REQUEST['bemail'] ) || !empty( $current_user->user_email ) ) ) {
//are we restricting emails for this level
global $pmpro_level;
$restrict_emails = pmpro_getOption("level_" . $pmpro_level->id . "_restrict_emails");
if( ! empty( $restrict_emails ) ) {
$restrict_emails = strtolower( str_replace( array( ";", ",", " " ), "\n", $restrict_emails ) );
if( ! empty( $current_user->user_email ) ) {
$needle = strtolower( $current_user->user_email );
} else {
$needle = strtolower( sanitize_email( $_REQUEST['bemail'] ) );
}
$haystack = explode( "\n", $restrict_emails );
array_walk( $haystack, function( &$val ) {
$val = trim($val);
return $val;
});
$okay = false;
foreach( $haystack as $hay ) {
if ( strpos( $needle, $hay ) !== false ) {
//Needle found in haystack - do nothing
$okay = true;
}
}
if( ! $okay ) {
global $pmpro_msg, $pmpro_msgt;
$pmpro_msg = "This membership level is restricted to certain users only. Make sure you've entered your email address correctly.";
$pmpro_msgt = "pmpro_error";
return $okay;
}
}
//are we restricting user names for this level
$restrict_usernames = pmpro_getOption("level_" . $pmpro_level->id . "_restrict_usernames");
if( ! empty( $restrict_usernames ) ) {
$restrict_usernames = strtolower( str_replace( array( ";", ",", " " ), "\n", $restrict_usernames ) );
if( ! empty( $current_user->user_login ) ) {
$needle = strtolower( $current_user->user_login );
} else {
$needle = strtolower( $_REQUEST['username'] );
}
$haystack = explode( "\n", $restrict_usernames );
array_walk( $haystack, function( &$val ) {
$val = trim($val);
return $val;
});
if( ! in_array( $needle, $haystack ) ) {
global $pmpro_msg, $pmpro_msgt;
$pmpro_msg = "This membership level is restricted to certain users only. Make sure you are logged into your existing account and using the proper username.";
$pmpro_msgt = "pmpro_error";
$okay = false;
}
}
}
return $okay;
}
add_filter( "pmpro_registration_checks", "mypmprorh_pmpro_registration_checks" );
@kimwhite
Copy link

Need to update the foreach on line 42. I've forked it here with the change https://gist.github.com/kimwhite/dcbee33061df3429fff529ad1304e57a
(thanks to DavidP for the help!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment