wpsl_gmap_api_params

  1. Description
  2. Parameters
  3. Usage
  4. Changelog
  5. Source File
  6. Related

Description

This filter allows you to change the parameters of the requests the plugin sends from your server to the Google Geocoding API. This happens when an address has to be turned into coordinates on the server. For example, when you save a location without coordinates, or save the Start location on the Map tab.

Since 3.0 this filter no longer changes how the map loads. It only changes the server-side geocode requests, and only for Google Maps. To change the parameters of the Maps JavaScript API, use the wpsl_gmaps_bootloader_params filter described below.

The parameters are a URL query string with a leading ampersand, for example &languageCode=en®ionCode=nl&key=YOUR_SERVER_KEY. The values are URL-encoded, and parameters without a value are left out.

Parameter Where the value comes from
languageCode The API response language on the API tab. When WPML, Polylang, Weglot or TranslatePress is active, the plugin uses its current language instead.
regionCode The Bias country on the API tab. When Country filtering is set to “Restrict results (hard)”, the country is only used when exactly one is selected. It’s left out while the API keys are validated.
key The Server key on the API tab.

You can find these settings on Store Locator > Settings > API.

You don’t need this filter for a multilingual site. The language is set automatically with WPML, Polylang, Weglot and TranslatePress, both for these requests and for the map. The one exception is described in the GDPR note below.

Parameters

$api_params - (string) The query string for the Geocoding API request, for example &languageCode=en®ionCode=nl&key=YOUR_SERVER_KEY.

Usage

This example makes the Geocoding API return its results in German, whatever language is set on the settings page. That affects, for example, the country name that is saved with a location.

add_filter( 'wpsl_gmap_api_params', 'custom_gmap_api_params' );
function custom_gmap_api_params( $api_params ) {
// Turn "&languageCode=en&regionCode=US&key=..." into an array.
parse_str( ltrim( $api_params, '&' ), $params );
// Get the Geocoding API results in German.
$params['languageCode'] = 'de';
return '&' . http_build_query( $params, '', '&', PHP_QUERY_RFC3986 );
}

Changing the Maps JavaScript API parameters

The map loads the Maps JavaScript API with Google’s bootstrap loader. The wpsl_gmaps_bootloader_params filter changes its parameters. The value is a string with one parameter per line, written as name: 'value',. The last line is v: 'quarterly', without a comma:

language: 'en',
region: 'nl',
key: 'YOUR_BROWSER_KEY',
v: 'quarterly'

Each line starts with a tab. When you add a parameter, put it in front of the string, so every line except the last one still ends with a comma. This example loads the weekly channel of the Maps JavaScript API instead of the quarterly one.

add_filter( 'wpsl_gmaps_bootloader_params', 'custom_gmaps_bootloader_params' );
function custom_gmaps_bootloader_params( $params ) {
// Load the weekly channel of the Maps JavaScript API instead of the quarterly one.
$params = str_replace( "v: 'quarterly'", "v: 'weekly'", $params );
return $params;
}
The wpsl_gmaps_bootloader_params filter doesn’t run when GDPR consent handled by on the GDPR tab is set to “WP Store Locator” or “Complianz”. The map then loads after consent with only the key, the version, and the language from the API response language setting. In that case the language of a multilingual plugin isn’t used for the map. You can change the version with the wpsl_script_versions filter, using the gmaps key.

Changelog

VersionDescription
3.0Moved to includes/core/functions/functions-maps.php. The filter now only changes the server-side requests to the Google Geocoding API v4. The parameters are languageCode, regionCode and key, and the values are URL-encoded. The Maps JavaScript API is loaded with Google's bootstrap loader, and its parameters are changed with the new wpsl_gmaps_bootloader_params filter. The language is set automatically for WPML, Polylang, Weglot and TranslatePress.
2.0Renamed from wpsl_gmap_api_attributes.
1.2.20Introduced.

Source File

This filter is located in /includes/core/functions/functions-maps.php.

Placement

The code needs to be placed in the functions.php file inside your active theme folder.