Enqueuing script is adding extra text for google maps

admin2025-06-06  2

Closed. This question is off-topic. It is not currently accepting answers.

Closed 6 years ago.

  • Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
  • Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?
Improve this question

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 &#038 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.

  • Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
  • Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?
Improve this question

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 &#038 and then the version were added:

<script type='text/javascript' src='https://maps.googleapis/maps/api/js?123123123123&#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.

Share Improve this question edited Nov 27, 2018 at 16:22 rudtek asked Nov 26, 2018 at 19:26 rudtekrudtek 6,4035 gold badges30 silver badges52 bronze badges 2
  • There's a link right there in the console that explains what the problem is. What's not clear? – Jacob Peattie Commented Nov 27, 2018 at 16:15
  • @JacobPeattie Thanks for your help. What's not clear is why i'm getting that message. I understand what that message means, but the api code is being sent Additional information added above as well. Thanks again. – rudtek Commented Nov 27, 2018 at 16:24
Add a comment  | 

2 Answers 2

Reset to default 4

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.

Reference

wp_register_script() docs

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749148049a316775.html

最新回复(0)