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
| Setting | Default | Description |
|---|---|---|
| Show Street Address | true | Display the primary street input. |
| Show Street Address 2 | true | Display the apartment/suite line. |
| Show City | true | Display the city input. |
| Show State / Province | true | Display the state or province input. |
| Show Postal / ZIP Code | true | Display the postal code input. |
| Show Country | true | Display the country input. |
| Return Format | array | array · 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>';
}
Google Maps link
$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));
}