Description
This filter changes the travel mode that’s used for the directions, for example walking instead of driving.
Each map service uses its own names for the travel modes. The filter receives the default travel mode of the active map service. If you return a value that isn’t allowed for the active map service, the default is used instead, without an error or notice. The values are case sensitive.
| Map service | Allowed values | Default |
|---|---|---|
| Google Maps | driving, bicycling, transit, walking |
driving |
| Mapbox | driving, driving-traffic, walking, cycling |
driving |
| OpenStreetMap | driving-car, driving-hgv, cycling-regular, cycling-road, cycling-mountain, cycling-electric, foot-walking, foot-hiking, wheelchair |
driving-car |
| Stadia Maps | driving, walking, cycling |
driving |
Where the travel mode is used:
- Google Maps: the route that’s drawn on the map. When Show the route on an external website? is checked in the User Experience settings, it’s used in the “Directions” link to Google Maps instead. The “Directions” link of the
[wpsl_address directions="true"]shortcode uses it too. - Mapbox: the route that’s drawn on the map. Mapbox always draws the route on the map.
- OpenStreetMap: the route that’s drawn on the map by Openrouteservice. This needs a valid Openrouteservice API key. The “Directions” link to openstreetmap.org doesn’t use the travel mode.
- Stadia Maps: the route that’s drawn on the map.
driving,walkingandcyclingare sent to Stadia Maps asauto,pedestrianandbicycle. The “Directions” link to openstreetmap.org doesn’t use the travel mode.
Usage
This example changes the travel mode to walking. The filter doesn’t tell you which map service is active, so the example checks it with wpsl_get_active_map_service() and returns the name that service uses.
| add_filter( 'wpsl_direction_travel_mode', 'custom_direction_travel_mode' ); | |
| function custom_direction_travel_mode( $mode ) { | |
| // Each map provider has its own name for walking directions. | |
| $walking = [ | |
| 'gmaps' => 'walking', | |
| 'mapbox' => 'walking', | |
| 'osm' => 'foot-walking', | |
| 'stadia' => 'walking', | |
| ]; | |
| $map_service = wpsl_get_active_map_service(); | |
| if ( isset( $walking[ $map_service ] ) ) { | |
| $mode = $walking[ $map_service ]; | |
| } | |
| return $mode; | |
| } |
If you only use one map service, you can return its value directly, for example return 'walking'; for Google Maps.
Changelog
| Version | Description |
|---|---|
| 3.0 | Moved to includes/core/functions/functions-geocode.php. The allowed values and the default now depend on the active map service (Google Maps, Mapbox, OpenStreetMap or Stadia Maps). A value that isn't allowed still falls back to the default without a notice. |
| 2.2.8 | Introduced. |
Source File
This filter is located in /includes/core/functions/functions-geocode.php.
Placement
The code needs to be placed in the functions.php file inside your active theme folder.