Home / Archive / MemberPress: Restrict Signups for US-Based Users
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Restrict Signups for US-Based Users

This code snippet will restrict signups for US-based Users on MemberPress registration pages. Additionally, it ensures that the country field is mandatory during signup

To update the error message displayed when a user from the restricted country tries to register, adjust the Sorry, signups are not allowed from the USA. error text on this line:

$errors[] = 'Sorry, signups are not allowed from the USA.';

To update the error message displayed when a user tries to register without selecting a country, adjust the "Country selection is required to proceed." error text on this line:

$errors[] = 'Country selection is required to proceed.';

Code Preview
php
<?php
function limit_signups_to_excluded_regions( $errors ) {
    // Check if the country field is set
    If ( isset( $_POST[ 'mepr-address-country' ] ) ) {
        $country = sanitize_text_field( wp_unslash( $_POST[ 'mepr-address-country' ] ) );
        // Exclude EU countries
        if ( $country == 'US' ) {
            $errors[] = 'Sorry, signups are not allowed from the USA.';
        }
    } else {
        // Handle missing country field if necessary
        $errors[] = 'Country selection is required to proceed.';
    }
    return $errors;
}
// Apply the filter to validate signup
add_filter( 'mepr-validate-signup', 'limit_signups_to_excluded_regions' );

Comments

Add a Comment