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 = 2757; // Change form ID
         
        var addressFieldID = 21; // Change address field ID 
         
        // Find field place by country
        var stateField = window.WPFormsGeolocationGooglePlacesAPI.findFieldPlaceByPolitical($( `#wpforms-${formID}-field_${addressFieldID}-state` )[0]);
 
            // Put a country code here according to this list: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
 
        // Set default autocomplete country to Canada
        stateField.autocomplete.setComponentRestrictions({
            'country': [ 'ca' ],
        }); 
    });
</script>
<?php
}
add_action( 'wpforms_wp_footer_end', 'wpf_dev_geo_autocomplete_default_country', 30 );

Comments

Add a Comment