Home / Archive / MemberPress: Prevent Customers from Certain States in the US from Registering
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Prevent Customers from Certain States in the US from Registering

This code snippet will prevent users based in specific US States from registering. For the code snippet to work, the Address Fields option must be enabled at Dashboard > MemberPress > Settings > Fields tab.

The example code will prevent customers from Alaska (AK) from registering for any membership.

The code should be modified to apply the restriction to another state. To change the state, the AK abbreviation for Alaska should be changed to the abbreviation of the desired state on this line:

$exclude_states = array('AK');

The code snippet will also add an error that will be displayed if a user based in the restricted US state tries to register.

The error "Sorry, but registration is not available in your state at this time." Message text can be changed on the following line:

$errors[] = 'Sorry, but registration is not available in your state at this time.';

Code Preview
php
<?php
function mepr_custom_limit_signups_by_state( $errors ) {
  $usr_country = sanitize_text_field( $_POST['mepr-address-country'] );
  $usr_state = sanitize_text_field( $_POST['mepr-address-state'] );
  $exclude_states = array('AK'); //Change this as needed or you can do a comma seperated list of state abbr.
  //If state in excluded list throw error
  if( $usr_country == 'US' && in_array( $usr_state, $exclude_states ) ) {
    $errors[] = 'Sorry, but registration is not available in your state at this time.'; //You can also change this message to suit your needs
  }
  return $errors;
}
add_filter( 'mepr-validate-signup', 'mepr_custom_limit_signups_by_state', 9 );

Comments

Add a Comment