method_mapbox_token
apply_filters( 'method_mapbox_token', string $token )
Supplies the Mapbox access token the whole site shares, so Mapbox Map blocks never need one of their own.
Description
method_mapbox_get_site_token() looks for a site-wide token in two places, in order: a callback on this filter, and — when none is attached — the token cmb2-mapbox keeps in Method’s options while its cmb2_mapbox_use_method switch is on. The filter is only applied when something is hooked to it (has_filter()), with an empty string as the starting value. While a token comes back, the Mapbox Map block uses it on every render, the editor is handed it as methodMapboxData.siteToken, and the block’s own Access token field is not offered. The cmb2-mapbox / method-tec-mapbox integration hooks this filter, which is how a site that already maps events shares one token with the block.
Parameters
$token (string) :
Empty. Return the public token (pk.…) the site should use, or an empty string to decline.
Source
File: lib/blocks/method-mapbox-map/method-mapbox-map.php, function method_mapbox_get_site_token()
if ( has_filter( 'method_mapbox_token' ) ) {
$token = trim( (string) apply_filters( 'method_mapbox_token', '' ) );
}
Usage
add_filter( 'method_mapbox_token', function () {
return defined( 'CHILD_MAPBOX_TOKEN' ) ? CHILD_MAPBOX_TOKEN : '';
} );
Notes
- The token is printed in the page markup (
data-map) and loaded into the editor, so it has to be a publicpk.token; the block warns about anything else. Restrict it by URL in the Mapbox account. - A token set here wins over a token stored in a block’s
accessTokenattribute; blocks that saved one before the site token existed keep it but stop using it. - Returning
''falls through to the cmb2-mapbox option, then to the block’s own token.
Related
- Mapbox Map block — the block
method_mapbox_map_styles— the styles the block offersmethod_mapbox_map_config— the config printed for the view script
Changelog
| Version | Description |
|---|---|
| 2.0.0-beta27 | Introduced, with the Mapbox Map block (fddeba0). |