Closed 6 years ago.
I'm setting up a google map on my site and I'm trying to set the enqueue my scripts up but am getting an error:
I have placed this code in my functions.php (actual api key not used here):
function rt_load_acf_map()
{
// Register my scripts for my theme:
wp_register_script( 'map-script', get_stylesheet_directory_uri() . '/js/acf-map.js', array( 'jquery' ) );
wp_register_script( 'googlemap', '' , '', '', false );
wp_register_script( 'slider-script', get_stylesheet_directory_uri() . '/js/slider.js', array( 'jquery' ) );
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( 'map-script' );
wp_enqueue_script( 'googlemap' );
if(is_front_page()){
wp_enqueue_script( 'slider-script' );
}
}
add_action( 'wp_enqueue_scripts', 'rt_load_acf_map' );
the map loads on the front end but with an overlay error:
This page can't load Google Maps correctly.
In my console I see this error:
Google Maps JavaScript API warning: NoApiKeys
and when I look at the page source i see that &
and then the version were added:
<script type='text/javascript' src=';#038;ver=4.9.8'></script>
EDITS
The api key is correct and working. That's not the issue.
Secondly, when I remove the wp_register_script and its wp_enqueue and just use this line:
wp_enqueue_script( 'google-maps-api', '//maps.googleapis/maps/api/js?key=123123123123', array(), '', true );
Everything works fine.
I can't figure out why it's not working as wone line and with the http in front of google maps url.
Closed. This question is off-topic. It is not currently accepting answers.
Closed 6 years ago.
I'm setting up a google map on my site and I'm trying to set the enqueue my scripts up but am getting an error:
I have placed this code in my functions.php (actual api key not used here):
function rt_load_acf_map()
{
// Register my scripts for my theme:
wp_register_script( 'map-script', get_stylesheet_directory_uri() . '/js/acf-map.js', array( 'jquery' ) );
wp_register_script( 'googlemap', 'https://maps.googleapis/maps/api/js?123123123123' , '', '', false );
wp_register_script( 'slider-script', get_stylesheet_directory_uri() . '/js/slider.js', array( 'jquery' ) );
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( 'map-script' );
wp_enqueue_script( 'googlemap' );
if(is_front_page()){
wp_enqueue_script( 'slider-script' );
}
}
add_action( 'wp_enqueue_scripts', 'rt_load_acf_map' );
the map loads on the front end but with an overlay error:
This page can't load Google Maps correctly.
In my console I see this error:
Google Maps JavaScript API warning: NoApiKeys https://developers.google/maps/documentation/javascript/error-messages#no-api-keys
and when I look at the page source i see that &
and then the version were added:
<script type='text/javascript' src='https://maps.googleapis/maps/api/js?123123123123&ver=4.9.8'></script>
EDITS
The api key is correct and working. That's not the issue.
Secondly, when I remove the wp_register_script and its wp_enqueue and just use this line:
wp_enqueue_script( 'google-maps-api', '//maps.googleapis/maps/api/js?key=123123123123', array(), '', true );
Everything works fine.
I can't figure out why it's not working as wone line and with the http in front of google maps url.
When you call google, the key
variable name is missing.
wp_register_script( 'googlemap', 'https://maps.googleapis/maps/api/js?123123123123' , '', '', false );
Should be :
wp_register_script( 'googlemap', '//maps.googleapis/maps/api/js?key=123123123123123123123123' , '', '', false );
Setting the $ver
parameter to false
(or, probably, ''
, as you've done), will append the current WordPress version to the <script>
tag. To have no version number appended, use null
instead:
wp_register_script(
'googlemap',
'https://maps.googleapis/maps/api/js?123123123123',
array(),
null,
false
);
(Also, the $deps
parameter should be an empty array rather than an empty string, which is also addressed in the code above.)
However: I don't think this is the actual problem you're having. The error message in your question suggests that you don't have an API key for Google Maps, which is an issue you'll have to sort out with Google.
wp_register_script()
docs