wpsl_js_settings

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

Description

This filter allows you to change the settings that the front-end scripts use. The plugin prints them on the page as the wpslSettings JavaScript object. They come from the settings pages and the [wpsl] shortcode attributes.

This is useful when you use the store locator on more than one page and need different settings on each page.

The settings are grouped in nested arrays. These are the groups and the keys you are most likely to change:

Group Useful keys
map zoomLevel, autoZoomLevel, startLatLng, scrollWheel, controlPosition, fitBounds. Google Maps only: type, typeControl, streetView, gestureHandling, mapId. Mapbox only: style.
search autoLocate (an array with enabled, format and trigger), autoLoad, autoComplete, distanceUnit, geoLocationTimeout, inputOnly, categoryFilterOnly, directionRedirect, restrictions
ux markerEffect, markerZoomTo, markerStreetView, newWindow, resetMap, mouseFocus, maxDropdownHeight
markers start, store, active, markerClusters, labels
api provider, language, and the provider-specific key and autocomplete settings
results moreInfoFields

There are also a few top-level keys, like url, storeUrl and gdpr. To see the full object for your setup, open the store locator page in your browser and type wpslSettings in the console.

In version 3.0 the keys moved into these groups. Flat keys from older snippets, like $settings['zoomLevel'] or $settings['mapOptions'], are ignored.

Parameters

$settings - (array) The front-end settings, grouped as shown above.

Usage

This example sets a different zoom level on the “car-dealers” and “mechanics” pages. zoomLevel is the zoom level when the map loads. autoZoomLevel is the max zoom level used when the map zooms to fit the markers, and for the “Zoom here” link. Both work with every map provider.

add_filter( 'wpsl_js_settings', 'custom_js_settings' );
function custom_js_settings( $settings ) {
// Use integers, the map scripts compare the zoom levels as numbers.
if ( is_page( 'car-dealers' ) ) {
$settings['map']['zoomLevel'] = 12;
$settings['map']['autoZoomLevel'] = 15;
} else if ( is_page( 'mechanics' ) ) {
$settings['map']['zoomLevel'] = 6;
$settings['map']['autoZoomLevel'] = 12;
}
return $settings;
}

Use integers for the zoom levels, not strings like '12'. With Google Maps, OpenStreetMap and Stadia Maps the map script compares the zoom levels as numbers. A string makes it also use zoomLevel as the max zoom level.

This example shows a satellite map without the map type control on the “contact-us” page. The map type only applies to Google Maps. The other map types are roadmap, hybrid and terrain.

add_filter( 'wpsl_js_settings', 'custom_js_map_type' );
function custom_js_map_type( $settings ) {
// Google Maps only. The map type is roadmap, satellite, hybrid or terrain.
if ( is_page( 'contact-us' ) ) {
$settings['map']['type'] = 'satellite';
$settings['map']['typeControl'] = 0;
}
return $settings;
}

The filter runs after the [wpsl] shortcode attributes are applied, so the filter wins. On a page with [wpsl map_type="hybrid"] the example above still shows a satellite map.

The filter also runs on pages with the [wpsl_map] shortcode. But a [wpsl_map] map has its own zoom, map_type and map_type_control attributes. They default to the settings and take priority over the map keys in this filter. To change those maps, use the shortcode attributes or the wpsl_map_shortcode_defaults filter.

Other map options

The old mapOptions key no longer exists. To set other options of the map itself, use the wpslMapOptions JavaScript hook. Add the callback with wp.hooks.addFilter in a script that runs before the store locator script. The easiest way is to attach it to the wp-hooks script with wp_add_inline_script(), as in the second example on the wpsl_gesture_handling page. The hook gets ( options, mapId, mapIndex ) and must return the options.

  • Google Maps: options is the MapOptions object. After the hook, the plugin sets mapTypeId from the map type. It also sets mapId when a Map ID is used, and gestureHandling or scrollwheel from the scroll wheel setting. Your values for these options are replaced, so change them with this PHP filter, the settings, or the wpsl_gesture_handling filter. In version 3.0.3, gestureHandling is always cooperative when Enable scroll wheel zooming? is off and the map uses a Map ID, which is the default.
  • Mapbox, OpenStreetMap and Stadia Maps: the first argument is an object with mapId and options. options is passed as it is to the Mapbox GL or Leaflet map.

Changelog

VersionDescription
3.0Moved to includes/frontend/assets/class-resources.php. The settings are now nested in groups like map, search, ux, markers and api. Flat keys and mapOptions are no longer read. Other map options go through the wpslMapOptions JavaScript hook. The filter also runs on pages with the [wpsl_map] shortcode.
2.2.8Introduced.

Source File

This filter is located in /includes/frontend/assets/class-resources.php.

Placement

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