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

Embed Snippet on Your Site

MemberPress: Restrict Signups for Northern Ireland-Based Users

This code snippet will restrict signups for Northern Ireland-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 Northern Ireland. error text on these lines:

$errors[] = 'Sorry, signups are not allowed from Northern Ireland.';

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 Northern Ireland under UK country code
        if( $country == 'GB' && isset( $_POST[ 'mepr-address-zip' ] ) ) {
            $postcode = strtoupper( trim( sanitize_text_field( $_POST[ 'mepr-address-zip' ] ) ) );
            if ( preg_match( '/^BT/i', $postcode ) ) { // Northern Ireland postcodes start with 'BT'
                $errors[] = 'Sorry, signups are not allowed from Northern Ireland.';
            }
        }
    } 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