Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active November 24, 2021 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cliffordp/a2ec320313afbc1ffb5f0e5ac654b7fb to your computer and use it in GitHub Desktop.
Save cliffordp/a2ec320313afbc1ffb5f0e5ac654b7fb to your computer and use it in GitHub Desktop.
Use one Google Maps API Key for JavaScript Map displays (The Events Calendar) and a different API Key for venue geolocation lookups (Events Calendar PRO).
<?php
/**
* Use one Google Maps API Key for JavaScript Map displays (The Events Calendar)
* and a different API Key for venue geolocation lookups (Events Calendar PRO).
*
* You need this in order to have API Key restrictions.
* Your "Maps JavaScript API" key should select "HTTP referrers (web sites)" restriction.
* Your "Geocoding API" key should select "IP addresses (web servers, cron jobs, etc.)" restriction.
* Screenshot: https://cl.ly/300k2I1s321N
*
* @link https://gist.github.com/cliffordp/a2ec320313afbc1ffb5f0e5ac654b7fb This snippet.
**/
add_filter( 'tribe_events_pro_geocode_request_url', 'ecp_separate_gmaps_api_key_for_geocoding' );
function ecp_separate_gmaps_api_key_for_geocoding( $api_url ) {
// TODO: !!! CHANGE THIS !!!
$geo_api_key = 'ABCxyz123';
$js_api_key = tribe_get_option( 'google_maps_js_api_key' );
$api_url = str_replace( 'key=' . $js_api_key, 'key=' . $geo_api_key, $api_url );
// esc_url() will break it so esc_url_raw() or don't escape at all
return esc_url_raw( $api_url );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment