Created
June 4, 2025 13:34
-
-
Save SitesByYogi/24ec7b18afd2cb463803b0610b9194b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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