Skip to content

Instantly share code, notes, and snippets.

@SitesByYogi
Created June 4, 2025 13:34
Show Gist options
  • Save SitesByYogi/24ec7b18afd2cb463803b0610b9194b5 to your computer and use it in GitHub Desktop.
Save SitesByYogi/24ec7b18afd2cb463803b0610b9194b5 to your computer and use it in GitHub Desktop.
<?php
function enqueue_google_maps_autocomplete() {
if (is_page('your-form-page-slug')) { // Optional: limit to the form page
?>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>
<script>
function initAutocomplete() {
const input = document.querySelector('.autocomplete-address');
if (!input) return;
const options = {
componentRestrictions: { country: "us" },
fields: ["address_components", "geometry", "icon", "name"],
strictBounds: false,
};
const autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.addListener("place_changed", () => {
const place = autocomplete.getPlace();
console.log(place); // Optional: store address data
});
}
document.addEventListener("DOMContentLoaded", function () {
initAutocomplete();
});
</script>
<?php
}
}
add_action('wp_footer', 'enqueue_google_maps_autocomplete');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment