Home / Archive / MemberPress: Restrict Email Patterns for All Memberships
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Restrict Email Patterns for All Memberships

This code snippet prevents users with specific email patterns from registering for any membership on the website.

When a user attempts to register with a restricted email pattern for any membership, the "Email addresses containing certain patterns are not allowed for this plan," error message will be displayed, preventing the registration from completing.

The sample list of specific email patterns ('xxx@gmail', 'xyz@', 'test@test') should be replaced with the actual patterns which should be restricted, in this line:

// Define the patterns to search for in email addresses
$blocked_email_patterns = array('xxx@gmail', 'xyz@', 'test@test');

To edit the error message, modify the "Email addresses containing certain patterns are not allowed for this plan," text in this line:

$errors[] = 'Email addresses containing certain patterns are not allowed for this plan.';

Code Preview
php
<?php
function mepr_restrict_email_patterns_for_all_plans($errors) {
    // Define the patterns to search for in email addresses
    $blocked_email_patterns = array( 'xxx@gmail', 'xyz@', 'test@test' );
    // Retrieve the email address from the form submission
    $email_address = isset( $_POST[ 'user_email' ] ) ? sanitize_email( trim( $_POST[ 'user_email' ] ) ) : '';
    // Check if the email contains any of the blocked patterns
    foreach ( $blocked_email_patterns as $pattern ) {
        if ( strpos( $email_address, $pattern ) !== false ) {
            $errors[] = 'Email addresses containing certain patterns are not allowed for this membership.';
            return $errors;
        }
    }
    return $errors;
}
add_filter( 'mepr-validate-signup', 'mepr_restrict_email_patterns_for_all_plans' );

Comments

Add a Comment