Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created February 2, 2026 07:50
Show Gist options
  • Select an option

  • Save xlplugins/a8e780f5d7a1bf8d4ddc23fbe3570276 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/a8e780f5d7a1bf8d4ddc23fbe3570276 to your computer and use it in GitHub Desktop.
uk address locality mapping with google address
<?php
add_action( 'wfacp_after_checkout_page_found', function () {
add_action( 'wp_footer', function () {
?>
<script>
window.addEventListener('load', function () {
(function ($) {
function extractValueByType(components, typeKey, nameType = 'long_name') {
const match = components.find(comp => comp.types.includes(typeKey));
return match ? match[nameType] : '';
}
wfacp_frontend.hooks.addFilter('wfacp_country_address_google_format', function (output, components) {
if (output._country !== 'GB') return output;
output._address_1 = [
extractValueByType(components, 'street_number'),
extractValueByType(components, 'route'),
].filter(Boolean).join(' ');
output._address_2 = [
extractValueByType(components, 'subpremise'),
extractValueByType(components, 'premise'),
extractValueByType(components, 'locality'),
].filter(Boolean).join(', ');
output._city = extractValueByType(components, 'postal_town');
output._state = extractValueByType(components, 'administrative_area_level_2');
output._postcode = extractValueByType(components, 'postal_code');
return output;
});
// Show address_2 field when filled
$(document.body).on('wfacp_gmap_address_filled', function(e, data) {
var prefix = data.type || 'billing';
if ($('#' + prefix + '_address_2').val()) {
$('#' + prefix + '_address_2_field').show();
$('#' + prefix + '_address_2_collapse_label').hide();
}
});
})(jQuery);
});
</script>
<?php
}, 100 );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment