Skip to content

Instantly share code, notes, and snippets.

@saqibsarwar
Last active May 8, 2022 18:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save saqibsarwar/8d34f74cc3ad6f252a062c91bac34707 to your computer and use it in GitHub Desktop.
Add Google Maps API key to enqueued script in WordPress
<?php
if ( ! function_exists( 'inspiry_google_maps_api_key' ) ) :
/**
* This function adds API key ( if provided in settings ) to google maps arguments
*/
function inspiry_google_maps_api_key( $google_map_arguments ) {
/* Get Google Maps API Key if available */
$google_maps_api_key = get_option( 'inspiry_google_maps_api_key' );
if ( ! empty( $google_maps_api_key ) ) {
$google_map_arguments[ 'key' ] = urlencode( $google_maps_api_key );
}
return $google_map_arguments;
}
add_filter( 'inspiry_google_map_arguments', 'inspiry_google_maps_api_key' );
endif;
<?php
/* default map query arguments */
$google_map_arguments = array ();
// Google Map API
wp_enqueue_script(
'google-map-api',
esc_url_raw(
add_query_arg(
apply_filters(
'inspiry_google_map_arguments',
$google_map_arguments
),
'//maps.google.com/maps/api/js'
)
),
array(),
'3.21',
false
);
@sungraiz
Copy link

sungraiz commented Jul 2, 2016

Good Work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment