wpsl_gmaps_bootloader_params

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

Description

This filter changes the parameters that are used to load the Google Maps JavaScript API. The plugin loads the API with Google’s bootstrap loader, and this filter changes the parameters that are passed to it. For example the language, the region, or the release channel in v.

The filter only works with Google Maps. It runs for the store locator, the [wpsl_map] shortcode, and the admin pages that show a Google map.

The language, region and key come from the API settings on Store Locator > Settings > API. Change them there first. When WPML, Polylang, Weglot or TranslatePress is active, the plugin uses the language of the current page instead of the API response language setting.

The filter doesn’t run when GDPR consent handled by on Store Locator > Settings > GDPR 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. The region and the language of a multilingual plugin aren’t used. You can change the version with the wpsl_script_versions filter, see the last example below. With “None” and “Borlabs Cookie”, the filter runs.

Parameters

$params (string) The parameters for the bootstrap loader, one per line.

Each line starts with a tab, is written as name: 'value', and ends with a new line. The plugin adds the lines in this order. A line is left out when its value is empty.

LineWhere the value comes from
language: 'en',The API response language setting, or the current language of WPML, Polylang, Weglot or TranslatePress.
region: 'nl',The Bias country setting. Left out when Country filtering is set to "Restrict results (hard)".
key: 'YOUR_BROWSER_KEY',The Browser key setting.
v: 'quarterly'Always added, as the last line, without a comma.

With all settings filled in, the string looks like this, where \t is a tab and \n a new line:

\tlanguage: 'en',\n\tregion: 'nl',\n\tkey: 'YOUR_BROWSER_KEY',\n\tv: 'quarterly'\n

The string is placed as it is between the braces of the JavaScript object that is passed to the loader. So every line except the last one must end with a comma, and the values need quotes.

Usage

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;
}

This example always loads the map in German, whatever the API response language setting or the multilingual plugin says. It removes the language line if there is one, and adds a new one at the start.

add_filter( 'wpsl_gmaps_bootloader_params', 'custom_gmaps_map_language' );
function custom_gmaps_map_language( $params ) {
// Remove the language line the plugin added, if there is one.
$params = preg_replace( "/^\tlanguage: '[^']*',\n/m", '', $params );
// Add the language as the first line. It ends with a comma because more lines follow.
$params = "\tlanguage: 'de',\n" . $params;
return $params;
}

To add another parameter, put a new line in front of the string, like the language line in the example, so every line except the last one still ends with a comma. Use the parameter names from Google’s documentation, for example authReferrerPolicy: 'origin',. That parameter only works when the website restriction of your browser key has no path.

With GDPR consent

When the GDPR consent is handled by “WP Store Locator” or “Complianz”, this filter doesn’t run. To load the weekly channel in that case, use the wpsl_script_versions filter with the gmaps key.

add_filter( 'wpsl_script_versions', 'custom_gmaps_script_version' );
function custom_gmaps_script_version( $versions ) {
// Load the weekly channel of the Maps JavaScript API when the map waits for GDPR consent.
$versions['gmaps'] = 'weekly';
return $versions;
}

This filter only changes the version when the map waits for consent. If you switch the GDPR setting now and then, add both examples.

Changelog

VersionDescription
3.0Introduced.

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.