Home / Archive / MemberPress: Disable Address Fields for a Specific Membership
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Disable Address Fields for a Specific Membership

Note: This code will not work if ReadyLaunch™ is enabled on the registration page.

The code will set the address fields as not required on the registration page of a specific membership and hide them.

The code needs to be adjusted by changing the dummy membership ID of 12 and 564 within the code with the ID of the user's membership(s). The code needs to be adjusted by changing the membership IDs on the following line:

if( is_single( array( 12, 564 ) ) ) {

In addition, the code can also be modified to exclude specific fields, leaving them visible and/or required. For example, to exclude the Country field from being hidden, the following code needs to be modified:

$hide_fields = array(
'mepr-address-zip',
'mepr-address-one',
'mepr-address-two',
'mepr-address-city',
'mepr-address-country'
);

It should be modified to this code:

$hide_fields = array(
'mepr-address-zip',
'mepr-address-one',
'mepr-address-two',
'mepr-address-city'
);

Next, to leave the Country field as required, the following lines should be removed from the code:

if( $error == 'Country is required.' ) {
unset( $errors[ $key ] );
}

Code Preview
php
<?php
function mepr_disable_address_fields( $fields ) {
    if ( is_single( array( 12, 564 ) ) ) {
        $hide_fields = array(
            'mepr-address-state',
            'mepr-address-zip',
            'mepr-address-one',
            'mepr-address-two',
            'mepr-address-city',
            'mepr-address-country',
        );
        foreach ( $fields as $key => $field ) {
            if ( isset( $field->field_key ) && in_array( $field->field_key, $hide_fields, true ) ) {
                unset( $fields[ $key ] );
            }
        }
    }
    return $fields;
}
add_filter( 'mepr_render_custom_fields', 'mepr_disable_address_fields' );
function disable_memberpress_state_validation( $errors ) {
  foreach( $errors as $key => $error ) {
    if( $error == 'Address Line 1 is required.' ) {
      unset( $errors[ $key ] );
    }
    if( $error == 'City is required.' ) {
      unset( $errors[ $key ] );
    }
    if( $error == 'Country is required.' ) {
      unset( $errors[ $key ] );
    }
    if( $error == 'State/Province is required.' ) {
      unset( $errors[ $key ] );
    }
    if( $error == 'Zip/Postal Code is required.' ) {
      unset( $errors[ $key ] );
    }
  }
  return $errors;
}
add_filter( 'mepr-validate-signup', 'disable_memberpress_state_validation' );
add_filter( 'mepr-validate-account', 'disable_memberpress_state_validation' );

Comments

Add a Comment