Last active
May 8, 2022 18:16
-
-
Save saqibsarwar/8d34f74cc3ad6f252a062c91bac34707 to your computer and use it in GitHub Desktop.
Add Google Maps API key to enqueued script in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good Work!