cliff
8/15/2018 - 4:00 AM

Use one Google Maps API Key for JavaScript Map displays (The Events Calendar) and a different API Key for venue geolocation lookups (Events

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 );
}