Loading Google Maps API with wp_enqueue_script

前端 未结 1 1641
青春惊慌失措
青春惊慌失措 2020-12-11 17:53

I\'m trying to load the Google Maps API using the following syntax:

add_action(\'admin_enqueue_scripts\', \'load_google_maps\');

...



        
相关标签:
1条回答
  • 2020-12-11 18:45

    I've got something similar in our code, and it's working fine (even encoded as &#038). I suspect your problem is that it's being double-encoded, as you already have &. Trying changing it to:

    $gmaps_url = 'http://maps.googleapis.com/maps/api/js?key=' . $key . '&sensor=false';
    

    For what it's worth, our (working) code is:

    wp_register_script('googlemaps', 'http://maps.googleapis.com/maps/api/js?' . $locale . '&key=' . GOOGLE_MAPS_V3_API_KEY . '&sensor=false', false, '3');
    wp_enqueue_script('googlemaps');
    

    ($locale in this case is set to hl=en)

    Edit

    Looks like the behaviour's changed in the latest version of WordPress - the above doesn't work (but I'll leave it for people on legacy versions). The only alternative I can see to echoing the script is to add a clean_url filter, something like this:

    add_filter('clean_url', 'so_handle_038', 99, 3);
    function so_handle_038($url, $original_url, $_context) {
        if (strstr($url, "googleapis.com") !== false) {
            $url = str_replace("&", "&", $url); // or $url = $original_url
        }
    
        return $url;
    }
    

    Pretty ugly, but perhaps marginally better than echoing the script, as it'll still use the WordPress dependency management.

    0 讨论(0)
提交回复
热议问题