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

Embed Snippet on Your Site

MemberPress: Restrict Email Patterns for Specific Memberships

This code snippet prevents users with specific email patterns from registering for any of the specified memberships.

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

The code snippet should be updated by replacing the dummy membership IDs of 123 and 456 with the IDs of actual memberships, in this line:

$restricted_plan_ids = array(123, 456);

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_specific_plans($errors) {
    // Define the patterns to search for in email addresses
    $blocked_email_patterns = array( 'xxx@gmail', 'xyz@', 'test@test' );
    // Define specific subscription plan IDs that require email restriction
    $restricted_plan_ids = array( 123, 456 ); // Replace with actual plan IDs
    // Retrieve the email address and membership ID from the form submission
    $email_address = isset( $_POST[ 'user_email' ] ) ? sanitize_email( trim( $_POST[ 'user_email' ] ) ) : '';
    $membership_id = isset( $_POST[ 'mepr_product_id' ] ) ? intval( $_POST[ 'mepr_product_id' ] ) : 0;
    // Check if the user is registering for a restricted plan
    if ( in_array( $membership_id, $restricted_plan_ids ) ) {
        // 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 plan.';
                return $errors;
            }
        }
    }
    return $errors;
}
add_filter( 'mepr-validate-signup', 'mepr_restrict_email_patterns_for_specific_plans' );

Comments

Add a Comment