Home / Archive / MemberPress: New and Improved Remove State From Registration (For countries that don’t have a drop down)
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: New and Improved Remove State From Registration (For countries that don’t have a drop down)

This code snippet will override the MemberPress script used to manage the state field on the MemberPress registration forms. If the country doesn’t have a list of states/provinces associated with it, the state field will be hidden when this code snippet is active.

Also, the field will be given a dummy value so that the address will still validate and taxes will still be calculated.

The code snippet works both with classic and ReadyLaunch™ registration pages.

Code Preview
php
<?php
 function mepr_remove_state_field() {
	global $post;
    $mepr_options = MeprOptions::fetch();
    $is_product_page = ( false !== ( $prd = MeprProduct::is_product_page( $post ) ) );
	
	if( $mepr_options->global_styles || $is_product_page ) {
?>
<script>
mepr_populate_states = function(obj) { (function($) {
  var country = obj.val();
  var form = obj.closest('.mepr-form');
  if(form == undefined) { return; }
  // Clean out all the options before re-populating
  form.find('.mepr-states-dropdown option').remove();
  var states_dropdown = form.find('.mepr-states-dropdown');
  var states_text = form.find('.mepr-states-text');
  // Ensure we've found a dropdown & text element
  if (states_dropdown.length <= 0 || states_text.length <= 0) { return; }
  // Grab fieldname and value
  var fieldname = states_dropdown.data('fieldname');
  var value = states_dropdown.data('value').toString();
  if(fieldname == undefined || value == undefined) { return; }
  // Clean up trailing whitespace
  fieldname = fieldname.replace(/^\s+/,'');
  fieldname = fieldname.replace(/\s+$/,'');
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  var required = !!obj.attr('required');
//if (MeprI18n.states[country] !== undefined && country == 'US') { //Use this instead of the line below to hide for all countries except the US
  if (MeprI18n.states[country] !== undefined) {
	$('.mepr_mepr-address-state').show();
	  
    states_dropdown.attr('name', fieldname);
    states_dropdown.show();
    states_text.removeAttr('name');
    states_text.hide();
    if (required) {
      states_text.removeAttr('required');
      states_dropdown.attr('required','');
    }
    states_dropdown.append('<option value="">' + MeprI18n.please_select_state + '</option>');
    for (var st in MeprI18n.states[country]) {
      var selected = (value===st ? ' selected="selected"' : '');
      states_dropdown.append('<option value="' + st + '" ' + selected + '>' + MeprI18n.states[country][st] + '</option>');
    }
  }
  else {
    states_dropdown.removeAttr('name');
    states_dropdown.hide();
	  
    states_text.attr('name', fieldname);
	states_text.val('(none)');
	$('.mepr_mepr-address-state').hide();
	  
    if (required) {
      states_dropdown.removeAttr('required');
      states_text.removeAttr('required');
    }
  }
})(jQuery)};
</script>
<?php 
	}
}
add_action( 'wp_footer', 'mepr_remove_state_field' );

Comments

Add a Comment