Home / Archive / MemberPress: Remove State Text Field
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Remove State Text Field

Removes the state text field from the registration and account pages if the country does not have a dropdown with states/provinces.

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 statesDropdown = form.find( '.mepr-states-dropdown' );
                var statesText = form.find( '.mepr-states-text' );
                // Ensure we've found a dropdown & text element
                if ( statesDropdown.length <= 0 || statesText.length <= 0 ) { return; }
                // Grab fieldname and value
                var fieldname = statesDropdown.data( 'fieldname' );
                var value = statesDropdown.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 ) {
                    $( '.mepr_mepr-address-state' ).show();
                    statesDropdown.attr( 'name', fieldname );
                    statesDropdown.show();
                    statesText.removeAttr( 'name' );
                    statesText.hide();
                    if (required) {
                        statesText.removeAttr( 'required' );
                        statesDropdown.attr( 'required','' );
                    }
                    statesDropdown.append( '<option value="">' + MeprI18n.please_select_state + '</option>' );
                    for ( var st in MeprI18n.states[country] ) {
                        var selected = ( value===st ? ' selected="selected"' : '' );
                        statesDropdown.append( '<option value="' + st + '" ' + selected + '>' + MeprI18n.states[country][st] + '</option>' );
                    }
                } else {
                    statesDropdown.removeAttr( 'name' );
                    statesDropdown.hide();
                    statesText.attr( 'name', fieldname );
                    statesText.val( '(none)' );
                    $( '.mepr_mepr-address-state' ).hide();
                    if ( required ) {
                        statesDropdown.removeAttr( 'required' );
                        statesText.removeAttr( 'required' );
                    }
                }
            })( jQuery )};
        </script>
        <?php
    }
}
add_action( 'wp_footer', 'mepr_remove_state_field' );

Comments

Add a Comment