method_mapbox_map_config
apply_filters( 'method_mapbox_map_config', array $config, array $block_attributes )
Filters the configuration a Mapbox Map block prints for its view script, just before it is printed.
Description
method_mapbox_get_map_config() assembles everything createMethodMap() (lib/blocks/utils/mapbox.js) needs — token, resolved style URL, center, zoom, the pin and the two toggles — from the block’s attributes, clamps the zoom (0–22) and pin scale (0.5–3), strips tags from the popup text and sanitises the pin color, then applies this filter. The result is JSON-encoded into the wrapper’s data-map attribute. This is the last word on what the page map does, so a child theme can change the zoom site-wide, swap the style by context, or move the center without touching the blocks.
Parameters
$config (array) : token (string), style (style URL), center ([ lng, lat ]), zoom (float), pin (lng, lat, scale, popup, and color when set), navigation (bool), cooperativeGestures (bool).
$block_attributes (array) :
The block’s attributes, for context (the stored mapStyle, className, and so on).
Source
File: lib/blocks/method-mapbox-map/method-mapbox-map.php, function method_mapbox_get_map_config()
return apply_filters( 'method_mapbox_map_config', $config, $block_attributes );
Usage
// Open every map one step further out than the block asks for,
// and use the dark style on the pages of one category.
add_filter( 'method_mapbox_map_config', function ( array $config, array $attributes ) {
$config['zoom'] = max( 0, $config['zoom'] - 1 );
if ( in_category( 'night-markets' ) ) {
$config['style'] = 'mapbox://styles/mapbox/dark-v11';
}
return $config;
}, 10, 2 );
Notes
- Runs on the page only. The editor preview builds its config in JavaScript from the same attributes, so a change made here is not seen in the canvas.
- Keys the view script does not know are ignored; removing
pinorcenterbreaks the map, since it is created centered on the pin. - The
aria-labelis built from the popup text before the filter runs, so changingpin.popuphere does not change the label.
Related
- Mapbox Map block — the block, with the printed markup
method_mapbox_token— the site tokenmethod_mapbox_map_styles— the style list
Changelog
| Version | Description |
|---|---|
| 2.0.0-beta27 | Introduced, with the Mapbox Map block (fddeba0). |