PDF for GravityView Hooks – gfpdf_gv_static_map_api_key
Description
Use this filter to set the Google Maps API key in code instead of the Static Maps Key setting (GravityKit > Settings > GravityView Maps). For example, read it from a constant in wp-config.php or an environment variable, or use a different key on staging and production.
Maps in PDFs are opt-in and are only drawn when a key is available. The value you return replaces the saved setting for every map in every PDF. Return an empty string to turn off all maps, including Entry Map fields and the Multiple Entries Map widget. See Maps in PDFs for the Google Cloud setup the key needs and how requests are billed.
Version
This hook was introduced in PDF for GravityView 2.0.
Arguments
The following arguments are passed to functions that use this hook:
$key | string
- The key saved in the Static Maps Key setting.
- Whitespace is trimmed from the value you return.
- The default value is
''when no key has been saved.
Usage
Use a key defined in wp-config.php (for example, define( 'MY_STATIC_MAPS_KEY', 'AIza...' );):
add_filter( 'gfpdf_gv_static_map_api_key', function( $key ) {
return defined( 'MY_STATIC_MAPS_KEY' ) ? MY_STATIC_MAPS_KEY : $key;
} );
Turn off maps in PDFs outside production, without changing the saved setting:
add_filter( 'gfpdf_gv_static_map_api_key', function( $key ) {
return wp_get_environment_type() === 'production' ? $key : '';
} );
The Check Key button tests the key entered in the setting, not the value from this filter. You can paste your key there to check it without saving. It reports a key restricted by HTTP referrer, a key without the Maps Static API enabled, and a server IP address missing from the key's IP restrictions.
Source Code
This filter is located in src/Pdf/Maps/ApiKey.php of the PDF for GravityView extension.