使用wp_enqueue_script加载Google Maps API [英] Loading Google Maps API with wp_enqueue_script

查看:104
本文介绍了使用wp_enqueue_script加载Google Maps API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下语法在WordPress管理类中加载Google地图API:

  add_action(' admin_enqueue_scripts',数组(& $ this,'load_google_maps')); 

...

 函数load_google_maps()
{
//实际的API密钥在选项页面中配置
$ key = get_option('google_maps_api_key');
$ gmaps_url ='http://maps.googleapis.com/maps/api/js?key='。 $键。 &放大器;放大器;传感器=假;
wp_enqueue_script('google-maps',$ gmaps_url,NULL,NULL);
}

WordPress正在逃避&到&#038。这实际上使Google服务器拒绝了该请求。当我直接在浏览器地址栏中输入& sensor = false结尾时,它会正常加载。



我看到了这种类型的错误WordPress trac系统: http://core.trac.wordpress.org/ticket/9243 但它被认为是无效的,管理员对该请求的回应表明#038方法没有问题。从Google的角度来看,这绝对不是好事。



我当然可以获得将HTML作为脚本标记的功能,但如果可能的话,我宁愿使用wp_enqueue_script系统。



任何人都知道这个解决方案吗?

干杯,

拉夫

解决方案

我在代码中有类似的东西,它工作正常(甚至编码为& ;#038 )。我怀疑你的问题是它是双重编码的,因为你已经有了& amp; 。尝试将其更改为:

  $ gmaps_url ='http://maps.googleapis.com/maps/api/js?key ='。 $键。 &安培;传感器=假; 

值得一提的是,我们的(工作)代码是:

  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 $ b

编辑 b $ b

看起来在最新版本的WordPress中行为发生了变化 - 上述功能无效(但我会将其留给旧版本的用户)。我可以看到的只是添加一个 clean_url 过滤器,如下所示:

  add_filter('clean_url','so_handle_038',99,3); 
函数so_handle_038($ url,$ original_url,$ _context){
if(strstr($ url,googleapis.com)!== false){
$ url = str_replace( &,&,$ url); //或$ url = $ original_url
}

return $ url;

$ / code>

非常丑陋,但可能稍微好于回应剧本,因为它会仍然使用WordPress依赖管理。


I'm trying to load the Google Maps API in a WordPress admin class using the following syntax:

add_action('admin_enqueue_scripts', array(&$this, 'load_google_maps'));

...

function load_google_maps()
{
  // The actual API key is configured in an options page
  $key = get_option('google_maps_api_key');
  $gmaps_url = 'http://maps.googleapis.com/maps/api/js?key=' . $key . '&sensor=false';
  wp_enqueue_script('google-maps', $gmaps_url, NULL, NULL);
}

WordPress is escaping the "&" to "&#038". This actually makes the Google server reject the request. When I type it directly into browser address bar with "&sensor=false" at the end, it loads fine.

I saw a bug of this kind mentioned in the WordPress trac system: http://core.trac.wordpress.org/ticket/9243 but it was dismissed as invalid, and the admin responding to the request showed somehow that the "&#038" approach was fine. It is definitely not fine from Google's point of view.

I could of course just get the function to echo the HTML as a script tag, but I'd rather use the wp_enqueue_script system if possible.

Anyone know of a solution to this?

Cheers,

raff

解决方案

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.

这篇关于使用wp_enqueue_script加载Google Maps API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆