2013-06-28 18 views
2

我不确定这是否可能,但我希望能够通过PHP以编程方式检索WordPress内置的jQuery版本号。通过PHP获取WordPress中的jQuery版本

我更喜欢使用wp_register_script()包含jQuery的CDN版本,然后使用WordPress的内置jQuery作为后备。

使用CDN版本的问题是,如果WordPress更新其内置版本的jQuery,CDN版本可能不匹配。所以我希望获取版本号(可能使用wp_default_scripts()),然后将其传递给wp_register_script()

关于我如何得到这个的任何想法?

+0

http://wordpress.org/plugins/wp-jquery-plus/。似乎这就是你需要的。 – elclanrs

回答

6

我从WP jQuery Plus借:

function hs_script_enqueuer() { 
    if(!is_admin()) { 

     // Enqueue so we can grab the built-in version 
     wp_enqueue_script('jquery'); 

     // Get jquery handle - WP 3.6 or newer changed the jQuery handle (once we're on 3.6+ we can remove this logic) 
     $jquery_handle = (version_compare($wp_version, '3.6-alpha1', '>=')) ? 'jquery-core' : 'jquery'; 

     // Get the WP built-in version 
     $wp_jquery_ver = $GLOBALS['wp_scripts']->registered[$jquery_handle]->ver; 

     // Just in case it doesn't work, add a fallback version 
     $jquery_ver = ($wp_jquery_ver == '') ? '1.8.3' : $wp_jquery_ver; 

     // De-register built-in jquery 
     wp_deregister_script('jquery'); 

     // Register CDN version 
     wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/'. $jquery_ver .'/jquery.min.js'); 

     // Enqueue new jquery 
     wp_enqueue_script('jquery'); 
    } 
} 
add_action('wp_enqueue_scripts', 'hs_script_enqueuer'); 

// Add jquery fallback if CDN is unavailable 
function jquery_fallback() { 
    echo '<script>window.jQuery || document.write(\'<script src="' . includes_url('js/jquery/jquery.js') . '"><\/script>\')</script>' . "\n"; 
} 
add_action('wp_head', 'jquery_fallback'); 
+1

嘿,很高兴你发现我的插件有用!想象一下,安装[插件](http://wordpress.org/plugins/wp-jquery-plus/)可能会更好,所以你总是可以使用任何新东西:) – Zach

0
if(! function_exists('register_jquery_script')){ 

    function register_jquery_script(){ 

     /* Get jquery handle - WP 3.6 or newer changed the jQuery handle (once we're on 3.6+ we can remove this logic) */ 
     $jquery_handle = (version_compare($wp_version, '3.6-alpha1', '>=')) ? 'jquery-core' : 'jquery'; 

     /* Get the WP built-in version */ 
     $wp_jquery_ver = $GLOBALS['wp_scripts']->registered[$jquery_handle]->ver; 

     /* Just in case it doesn't work, add a fallback version */ 
     $jquery_ver = ($wp_jquery_ver == '') ? '1.8.3' : $wp_jquery_ver; 

     /* the URL to check CDN accessibility */ 
     $url = 'http://ajax.googleapis.com/ajax/libs/jquery/' . $jquery_ver . '/jquery.min.js'; 

     /* test CDN accessibility */ 
     $test_url = wp_remote_fopen($url); 

     /* deregisters the default WordPress jQuery */ 
     wp_enqueue_script('jquery'); 
     wp_deregister_script('jquery'); 

     /* if CDN available - load it */ 
     if($test_url !== false) { 
      wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/'. $jquery_ver .'/jquery.min.js', false , false , true); 
     } 

     /* if CDN unavailable - load local jquery copy */ 
     else{ 
      wp_register_script('jquery', get_template_directory_uri() . '/js/jquery.min.js', __FILE__, false, false, true); 
     } 

     wp_enqueue_script('jquery'); 

    } 

    add_action('wp_enqueue_scripts','register_jquery_script'); 

} 

,你必须确保你加载本地的jQuery到template_directory/JS与名称/jquery.min.js

我在默认情况下,将加载wp_footer()中的所有脚本用于页面加载性能。 如果您在加载wp_header()的JS脚本,你会改变对行内最后真正

wp_register_script('jquery' , $src , false , false , false); 

享受吧!

+0

虽然我喜欢*想法*以这种方式测试URL,不幸的是需要指出的是,在这种情况下它不可靠,因为'wp_remote_fopen'会测试你的服务器对CDN资源的访问,*不是*用户/客户端的访问 - 所以脚本回退方法实际上是因为这个更可靠。 – majick