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

Embed Snippet on Your Site

MemberPress: Restrict Signups for EU-Based Users

This code snippet will restrict signups for EU-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 EU countries. error text on this line:

$errors[] = 'Sorry, signups are not allowed from EU countries.';

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.';

The list of excluded countries can be modified by removing any two-letter country code on these lines:

$eu_countries = array(
'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE',
'EL', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT',
'RO', 'SK', 'SI', 'ES', 'SE'
);

Removing any two-letter country code will allow registration for users based in that country.

Code Preview
php
<?php
function limit_signups_to_excluded_regions( $errors ) {
    // List of EU country codes
    $eu_countries = array(
        'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE',
        'EL', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT',
        'RO', 'SK', 'SI', 'ES', 'SE'
    );
    // 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 ( in_array( $country, $eu_countries ) ) {
            $errors[] = 'Sorry, signups are not allowed from EU countries.';
        }
    } 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