Formatting an address in GravityExport
By default an address field is rendered by adding every value on a newline. Something like:
Address 1 City State Zipcode Country
This is not always what you want. Let’s rearrange it a bit to be more readable, into this format:
Address 1 City, State Zipcode Country
Recipe
/**
* Disables separated address fields in GravityExport (GFExcel) exports.
*
* By returning false, this ensures the address is exported as a single cell value
* instead of being split into multiple columns.
*
* @since 1.0.0
*
* @return bool False to disable separated address fields.
*/
add_filter( 'gfexcel_field_separated_address', '__return_false' );
/**
* Formats exported address fields according to a custom format.
*
* This hook allows you to customize how address data appears in exports.
* Each address part can be referenced by wrapping the key in square brackets,
* for example: `[address_1]`, `[city]`, `[state]`, `[zipcode]`.
*
* @since 1.0.0
*
* @param string $original_value The current value of the address field.
* @param array $entry The full Gravity Forms entry array.
* @param GF_Field $field The field object instance for the current address field.
*
* @return string The formatted address string.
*/
add_filter(
'gfexcel_field_value_address',
function ( $original_value, $entry, $field ) {
// Define the desired address format.
// You can customize this pattern as needed.
$format = "[address_1]\n[city], [state] [zipcode]\n[country]";
// The keys below correspond to field input names.
$map = [ 'address_1', 'address_2', 'city', 'state', 'zipcode', 'country' ];
// Replace placeholders in the format string with actual values from the entry.
return array_reduce(
array_keys( $map ),
function ( $output, $key ) use ( $map, $entry, $field ) {
return str_replace(
"[{$map[$key]}]",
$entry[ $field->id . '.' . ( $key + 1 ) ],
$output
);
},
$format
);
},
10,
3
);