Home › Forums › ACF PRO › Google Maps enqueue scripts › Reply To: Google Maps enqueue scripts
I use this in one of my in house themes. It simply checks if the current page has a locations field which would require a google map. We then check if google_maps_api function exists in case we forgot to include it. then we enqueue the script to be added in the footer. You can change true to false if you want it in the header.
add_action( 'wp_enqueue_scripts', 'wpb_adding_scripts' );
function wpb_adding_scripts() {
global $post;
if(!empty(get_field('locations',$post->ID)){
if(function_exists('google_maps_api')){
$apiKey=google_maps_api();
if($apiKey){
wp_register_script('googleMapsAPI',
'https://maps.googleapis.com/maps/api/js?key='.$apiKey,
'',
true
);
wp_enqueue_script('googleMapsAPI');
}
}
}
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.