Restrict Address Autocomplete to a Specific Country
-
Hello,
I have implemented this snippet to get the address autocomplete to a spesific country. This code works only on an “address” field.
When I use it on a “single line text” field with address autocomplete enabled it wont work. Do you have any suggestions for how I can edit the snippet to make it work on “single line text” field as well?
/** * 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 );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Restrict Address Autocomplete to a Specific Country’ is closed to new replies.