Using Gravity Forms ‘gform_address_display_format’ PHP action

The gform_address_display_format filter allows you to change the way addresses are formatted in Gravity Forms.

Usage

add_filter('gform_address_display_format', 'change_address', 10, 2);

Parameters

  • $format (string): The format to be filtered. Possible values are default and zip_before_city.
    • default: Addresses are formatted in the following order: Street, Street2, City, State, Zip Code, Country.
    • zip_before_city: Addresses are formatted in the following order: Street, Street2, Zip, City, State, Country.
  • $field (Field Object): The current field.

More information

See Gravity Forms Docs: gform_address_display_format

Examples

Change address format to zip_before_city

This example demonstrates how to change the address format to zip_before_city.

add_filter('gform_address_display_format', 'address_format', 10, 2);

function address_format($format, $field) {
    return 'zip_before_city';
}

Custom address format

This example demonstrates how to create a custom address format. The custom format will display the address in the following order: Street, City, Zip, State, Country.

add_filter('gform_address_display_format', 'custom_address_format', 10, 2);

function custom_address_format($format, $field) {
    return 'custom';
}

add_filter('gform_address_zip_before_city', 'custom_format', 10, 1);

function custom_format($address) {
    $address_formatted = $address['street'] . '<br />' .
                         $address['city'] . ', ' .
                         $address['zip'] . '<br />' .
                         $address['state'] . '<br />' .
                         $address['country'];
    return $address_formatted;
}

Conditionally change address format

This example demonstrates how to change the address format based on the form ID.

add_filter('gform_address_display_format', 'address_format_by_form', 10, 2);

function address_format_by_form($format, $field) {
    if ($field->formId == 1) {
        return 'zip_before_city';
    }
    return $format;
}

Conditionally change address format by field ID

This example demonstrates how to change the address format based on the field ID.

add_filter('gform_address_display_format', 'address_format_by_field', 10, 2);

function address_format_by_field($format, $field) {
    if ($field->id == 5) {
        return 'zip_before_city';
    }
    return $format;
}