Address

A structured address input with up to six toggleable parts: street, street line 2, city, state/province, postal code, and country. Each sub-field can be shown or hidden in the field settings. Returns either an associative array of parts or a single formatted one-line string.

Settings

SettingDefaultDescription
Show Street AddresstrueDisplay the primary street input.
Show Street Address 2trueDisplay the apartment/suite line.
Show CitytrueDisplay the city input.
Show State / ProvincetrueDisplay the state or province input.
Show Postal / ZIP CodetrueDisplay the postal code input.
Show CountrytrueDisplay the country input.
Return Formatarrayarray · string

Return values

return_format: 'array'

Returns an associative array. Keys for hidden sub-fields are still present but empty.

$address = get_field('billing_address');
// [
//   'street'  => '123 Main St',
//   'street2' => 'Suite 400',
//   'city'    => 'New York',
//   'state'   => 'NY',
//   'zip'     => '10001',
//   'country' => 'USA',
// ]

return_format: 'string'

Returns a comma-joined one-line string, skipping empty parts.

$address = get_field('billing_address');
// "123 Main St, Suite 400, New York, NY 10001, USA"

Usage

Displaying each part separately

$address = get_field('office_address');

if ($address) {
    echo '<address>';
    if ($address['street'])  echo '<span>' . esc_html($address['street'])  . '</span>';
    if ($address['street2']) echo '<span>' . esc_html($address['street2']) . '</span>';
    echo '<span>';
    if ($address['city'])    echo esc_html($address['city']);
    if ($address['state'])   echo ', ' . esc_html($address['state']);
    if ($address['zip'])     echo ' ' . esc_html($address['zip']);
    echo '</span>';
    if ($address['country']) echo '<span>' . esc_html($address['country']) . '</span>';
    echo '</address>';
}
$address = get_field('location_address'); // return_format: 'string'
if ($address) {
    $query = urlencode($address);
    echo '<a href="https://maps.google.com/?q=' . esc_attr($query) . '" target="_blank" rel="noopener">View on map</a>';
}

Structured data (Schema.org PostalAddress)

$address = get_field('business_address');

if ($address) {
    $schema = [
        '@type'           => 'PostalAddress',
        'streetAddress'   => trim(($address['street'] ?? '') . ' ' . ($address['street2'] ?? '')),
        'addressLocality' => $address['city']    ?? '',
        'addressRegion'   => $address['state']   ?? '',
        'postalCode'      => $address['zip']     ?? '',
        'addressCountry'  => $address['country'] ?? '',
    ];
    echo '<script type="application/ld+json">' . wp_json_encode($schema) . '</script>';
}

Formatting for a single-line display

$address = get_field('shipping_address'); // return_format: 'array'
if ($address) {
    $parts = array_filter([
        $address['street'],
        $address['city'],
        $address['state'],
        $address['country'],
    ]);
    echo esc_html(implode(', ', $parts));
}