Support

Account

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