How to add a postal address

Jeff Porter

New member
I am using the ACF fields plugin to add fields to a WP site, but I am struggling with how to include the address.

I have the following fields:

$addr1
$addr2
$town_or_city
$county
$postcode

addr1 and addr2 may or may not be included, according to the address provided. Usually it is only addr2 that is not needed, but I want to output the full address, comma delimited, but excluding addr1 or addr2 when they are not included. Any help would be appreciated.
 
Have you tried putting the whole address into one text field? Implement Google Autocomplete API to auto fill the text field as you type.

Place the following into you functions.php file:
Code:
// functions.php

// Enqueue Google Autocomplete API and custom script
function enqueue_google_autocomplete() {
    wp_enqueue_script('google-autocomplete', 'https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places', null, null, true);
    wp_enqueue_script('acf-autocomplete', get_template_directory_uri() . '/js/acf-autocomplete.js', array('google-autocomplete'), null, true);
}
add_action('wp_enqueue_scripts', 'enqueue_google_autocomplete');

// Register ACF field
if( function_exists('acf_add_local_field_group') ) {
    acf_add_local_field_group(array(
        'key' => 'group_location',
        'title' => 'Location',
        'fields' => array(
            array(
                'key' => 'field_location',
                'label' => 'Location',
                'name' => 'location',
                'type' => 'text',
                'instructions' => 'Start typing a location...',
                'required' => 1,
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'post_type',
                    'operator' => '==',
                    'value' => 'post',
                ),
            ),
        ),
    ));
}
Here is the JS:
Code:
// acf-autocomplete.js

document.addEventListener('DOMContentLoaded', function() {
    const input = document.querySelector('input[name="location"]');
    
    if (input) {
        const autocomplete = new google.maps.places.Autocomplete(input);
        
        autocomplete.addListener('place_changed', function() {
            const place = autocomplete.getPlace();
            if (place && place.geometry) {
                console.log('Selected place:', place);
            }
        });
    }
});

Hope it helps.
 
No, I haven’t tried that, @Moorcam.

I’ve tried this approach, but then I lose the commas:


<?php $address_1 = the_field('address_1'); ?>
<?php if ( $address_1 == "" )
{
echo "";
}
else {
echo "<?php the_field('address_1'); ?>,";
}
?>
<?php $address_2 = the_field('address_2'); ?>
<?php if ( $address_2 == "" )
{
echo "";
}
else {
echo "<?php the_field('address_2'); ?>,"; // Note comma excluded
}
?>
 
No, I haven’t tried that, @Moorcam.

I’ve tried this approach, but then I lose the commas:


<?php $address_1 = the_field('address_1'); ?>
<?php if ( $address_1 == "" )
{
echo "";
}
else {
echo "<?php the_field('address_1'); ?>,";
}
?>
<?php $address_2 = the_field('address_2'); ?>
<?php if ( $address_2 == "" )
{
echo "";
}
else {
echo "<?php the_field('address_2'); ?>,"; // Note comma excluded
}
?>
The code contains a logical error in how it attempts to display the values of address_1 and address_2. Instead of outputting the actual values, it is echoing the PHP code as a string, which will not yield the desired result.
The issue arises from the use of double quotes around the field.
Try this:
Code:
<?php 
$address_1 = the_field('address_1'); 
if ($address_1 == "") {
    echo "";
} else {
    echo $address_1 . ",";
}
?>

<?php 
$address_2 = the_field('address_2'); 
if ($address_2 == "") {
    echo "";
} else {
    echo $address_2 . ","; // Note comma included
}
?>
Hope it works. Untested of course.
 
The code contains a logical error in how it attempts to display the values of address_1 and address_2. Instead of outputting the actual values, it is echoing the PHP code as a string, which will not yield the desired result.
The issue arises from the use of double quotes around the field.
Try this:
Code:
<?php
$address_1 = the_field('address_1');
if ($address_1 == "") {
    echo "";
} else {
    echo $address_1 . ",";
}
?>

<?php
$address_2 = the_field('address_2');
if ($address_2 == "") {
    echo "";
} else {
    echo $address_2 . ","; // Note comma included
}
?>
Hope it works. Untested of course.
No, it still outputs address_1 and address_2 without the commas.

Thanks for trying.
 
No, it still outputs address_1 and address_2 without the commas.

Thanks for trying.
Try this...
Code:
<?php 
$address_1 = the_field('address_1'); 
$address_2 = the_field('address_2'); 

$addresses = array_filter([$address_1, $address_2]); // Filter out empty addresses
echo implode(", ", $addresses) . (count($addresses) > 0 ? "," : ""); // Join addresses with a comma
?>
 
That doesn’t work either. The end result is shown below, where Main Road = address_1 and Titchwell = address_2. I’ve been trying it with implode
but with no joy so far.

Screenshot 2025-01-01 at 13.13.11.png
 
That doesn’t work either. The end result is shown below, where Main Road = address_1 and Titchwell = address_2. I’ve been trying it with implode
but with no joy so far.

View attachment 995
Ok. I have tested this and it adds a comma after address_1
Code:
<?php 
$address_1 = the_field('address_1');
$address_2 = the_field('address_2');

if (!empty($address_1)) {    echo $address_1 . ",";}
if (!empty($address_2)) {    echo $address_2 . ",";}
?>
Try that.
 
I need both addr1 and addr2 to have a comma after them.

So that when a business address is presented on a single line/in a single paragraph it reads okay. As follows

CO NAME, ADDR1, ADDR2, TOWN, COUNTY, POSTCODE

Normally, they will include at least an ADDR1, which requires a comma after it. But, occasionally, they require an ADDR2. Or, as I have discovered, there is no ADDR1 or ADDR2, so both must be excluded, including their commas.
 
I need both addr1 and addr2 to have a comma after them.

So that when a business address is presented on a single line/in a single paragraph it reads okay. As follows

CO NAME, ADDR1, ADDR2, TOWN, COUNTY, POSTCODE

Normally, they will include at least an ADDR1, which requires a comma after it. But, occasionally, they require an ADDR2. Or, as I have discovered, there is no ADDR1 or ADDR2, so both must be excluded, including their commas.
Try this.
Code:
<?php 
$address_1 = the_field('address_1');
$address_2 = the_field('address_2');

$addresses = array_filter([$address_1, $address_2]);

if (!empty($addresses)) {
    echo implode(',', $addresses) . ',';
}
?>
I have changed how the addresses are stored so utilised an array and the array_filter function. It allows us to filter out any empty values.
The implode function is then used to concatenate the non-empty addresses with a comma as the separator. Finally, an additional comma is appended at the end of the output, ensuring that both addresses are followed by a comma if they exist.
Hopefully this helps. :)
Cheers,
Dan
 
Thanks, Dan. But that doesn’t give the desired result either. I’ll have to revert to adding commas manually.
 

Attachments

  • Screenshot 2025-01-08 at 15.18.57.png
    Screenshot 2025-01-08 at 15.18.57.png
    7 KB · Views: 2
Back
Top