通过PHP在WordPress中获取jQuery版本

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了通过PHP在WordPress中获取jQuery版本脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我不确定这是否可行,但我希望我可以通过 PHP编程方式检索wordpress的内置jquery版本号.

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

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

关于我如何做到一点的任何想法?

解决方法

我从 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' );

脚本宝典总结

以上是脚本宝典为你收集整理的通过PHP在WordPress中获取jQuery版本全部内容,希望文章能够帮你解决通过PHP在WordPress中获取jQuery版本所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。