Home / Admin / Restricting Address Autocomplete to a Specific Country
Duplicate Snippet

Embed Snippet on Your Site

Restricting Address Autocomplete to a Specific Country

This snippet is used to modify the default behavior of the address autocomplete feature in WPForms. It lets you restrict the autocomplete to any country you specify in the snippet.

10+
Code Preview
php
<?php
/**
 * Restrict address autocomplete to a specific country
 *
 * @link https://wpforms.com/developers/how-to-restrict-address-autocomplete-to-a-specific-country/
 */
 
function wpf_dev_geo_autocomplete_default_country() {
    ?>
    <script type="text/javascript">
        jQuery(function($){
            var formID = '1000'; // Change form ID
            var addressFieldID = '25'; // Change address field ID
            var inputFieldElement = document.getElementById('wpforms-' + formID + '-field_' + addressFieldID);
            if (inputFieldElement) {
                var autocomplete = new google.maps.places.Autocomplete(inputFieldElement);
                autocomplete.setComponentRestrictions({ 'country': ['au'] });
            }
        });
    </script>
    <?php
}
add_action('wpforms_wp_footer_end', 'wpf_dev_geo_autocomplete_default_country', 30);

Comments

Add a Comment