How to Change the Address Field Sublabels

Introduction

Would you like to change the Address field sublabels in WPForms? These sublabels let users know what type of information that the user is expected to fill in. In this tutorial, we’ll walk you through how to change these sublabels using PHP.

By default the Address form field will display additional fields. Each of those fields have their own labels called sublabels.

By default the Address form field will bring in additional fields.  Each of those fields have their own labels called sublabels.

Creating the form

We’ll begin by creating the form and adding our fields which will include the Address form field.

If you need any assistance in creating your form, please check out this documentation.

create the form and add your fields

Adding the snippet

It’s now time to add the snippet to your site.

If you need any assistance in where and how to add snippets to your site, please review this tutorial.

Change the sublabels for the US address scheme only

The below code snippet will allow you to change the sublabels on your Address field for the US scheme.

/**
 * Change the sublabels for the Address field for the US Address Scheme.
 *
 * @link https://wpforms.com/developers/how-to-change-the-address-field-sublabels/
 */

function wpf_dev_address_field_properties( $properties, $field, $form_data ) {
  
  // check for address scheme
  if ( $field[ 'scheme' ] === 'us' ){

	  // Change sublabel values
	  $properties[ 'inputs' ][ 'address1' ][ 'sublabel' ][ 'value' ] = __( 'Street Address' );
	  $properties[ 'inputs' ][ 'address2' ][ 'sublabel' ][ 'value' ] = __( 'Apartment #' );
	  $properties[ 'inputs' ][ 'city' ][ 'sublabel' ][ 'value' ] = __( 'City' );
	  $properties[ 'inputs' ][ 'state' ][ 'sublabel' ][ 'value' ] = __( 'State' );
	  $properties[ 'inputs' ][ 'postal' ][ 'sublabel' ][ 'value' ] = __( 'Zip' );
  	}

    return $properties;
}

add_filter( 'wpforms_field_properties_address' , 'wpf_dev_address_field_properties', 10, 3 );

Change the sublabels for the International address scheme only

The below code snippet will allow you to change the sublabels on your Address field for the International scheme.

/**
 * Change the sublabels for the Address field for the International Address Scheme.
 *
 * @link https://wpforms.com/developers/how-to-change-the-address-field-sublabels/
 */

function wpf_dev_address_field_properties( $properties, $field, $form_data ) {
  
  // check for address scheme
  if ( $field['scheme'] === 'international' ){

	  // Change sublabel values
	  $properties[ 'inputs' ][ 'address1' ][ 'sublabel' ][ 'value' ] = __( 'Address' );
	  $properties[ 'inputs' ][ 'address2' ][ 'sublabel' ][ 'value' ] = __( 'Apartment / Suite #' );
	  $properties[ 'inputs' ][ 'city' ][ 'sublabel' ][ 'value' ] = __( 'City' );
	  $properties[ 'inputs' ][ 'state' ][ 'sublabel' ][ 'value' ] = __( 'State / Province / Region' );
	  $properties[ 'inputs' ][ 'postal' ][ 'sublabel' ][ 'value' ] = __( 'Postal Code' );
	  $properties[ 'inputs' ][ 'country' ][ 'sublabel' ][ 'value' ] = __( 'Country' );
  	}

    return $properties;
}

add_filter( 'wpforms_field_properties_address' , 'wpf_dev_address_field_properties', 10, 3 );

now you can change the address field sublabels to anything you would like

And that’s it! Would you like to create additional address schemes to be used in your Address field? Take a look at our article on How to Create Additional Schemes for the Address Field.

Filter Reference: wpforms_field_properties