Let me show you an easy way to limit a country in location search dropdown and address field auto-suggest.
This mod will work only for WordPress themes ClassiPress v4.1.0+, Vantage v4.2.0+ and JobRoller v1.9.0+, which implement the AppMaps Geo framework by AppThemes. The Google Maps is the only supported AppMaps provider for this mod. Means, it will not work with Bing or MapQuest providers.
AppThemes themes implementing AppMaps framework have option “Region Biasing”, which limits the country in location dropdown in soft way. It will suggest locations related to the selected country in the first places, but still, allow the other countries options select at the end of the list.
However, if you want to limit the country in strict way, i.e. do not allow to enter any other countries except selected, you would need to add the following code in your theme JS scripts file.
( function( $ ) {
$( document ).on( 'appaddressautocompleteready', function( e, input ) {
var input = $( e.target ).data('appthemesAppAddressAutocomplete' );
input.api.setComponentRestrictions(
{ 'country': [ 'ru', 'us', 'it', 'fr' ] }
);
} );
} )( jQuery );
Alternatively, you can add this JS code via Code Snippets plugin. But you will need to wrap it in following way:
add_action( 'wp_head', function () { ?>
<script>
( function( $ ) {
$( document ).on( 'appaddressautocompleteready', function( e, input ) {
var input = $( e.target ).data('appthemesAppAddressAutocomplete' );
input.api.setComponentRestrictions(
{ 'country': [ 'ru', 'us', 'it', 'fr' ] }
);
} );
} )( jQuery );
</script>
<?php } );
As you see, the countries in the sample presented as two-letter region codes (ru
, us
, it
, fr
). So you can limit location in dropdown to these four countries. Edit the list with your choice. Don’t leave trailing comma!
See below how it works.
ClassiPress location search dropdown before restriction ClassiPress location search dropdown restricted to Russia Vantage listing address field restricted to Russia
If you’re using ClassiPress theme and want to replace search location filed with a custom one, read our post.