Skip to main content

Wordpress replace jQuery with the Google API version + FallBack to local

__________________________________________________________
https://wordpress.stackexchange.com/questions/12448/how-to-replace-regular-jquery-calls-with-cdn-calls-from-google/12450

Notice that I'm still using WordPress' dependency management system. Also, sets HTTPS vs HTTP using is_ssl() to avoid mixed content errors, and lets WordPress dictate version number.


//// REPLACE JQUERY WITH THE GOOGLE API version +++ get local wp version
add_action( 'init', function(){
    //Only act if is admin
    if (  ! is_admin()) {
        //set protocol dynamically
        if( is_ssl() ){
            $protocol = 'https';
        }else {
            $protocol = 'http';
        }

        /** @var WP_Scripts $wp_scripts */
        global  $wp_scripts;
        /** @var _WP_Dependency $core */
        $core = $wp_scripts->registered[ 'jquery-core' ];
        $core_version = $core->ver;
        $core_version = str_replace("-wp", "", $core_version);
        $core->src = "$protocol://ajax.googleapis.com/ajax/libs/jquery/$core_version/jquery.min.js";

        ///Use jQuery migrate if WP_DEBUG
        if ( WP_DEBUG ) {
            /** @var _WP_Dependency $migrate */
            $migrate         = $wp_scripts->registered[ 'jquery-migrate' ];
            $migrate_version = $migrate->ver;
            $migrate_version = str_replace("-wp", "", $migrate_version);
            $migrate->src    = "$protocol://cdn.jsdelivr.net/jquery.migrate/$migrate_version/jquery-migrate.min.js";
        }else{
            /** @var _WP_Dependency $jquery */
            $jquery = $wp_scripts->registered[ 'jquery' ];
            $jquery->deps = [ 'jquery-core' ];
        }

    }


}, 11 );
_____________________________________________________

/**
 * Print fallback script right after jQuery is printed from queue.
 *
 * @param   string $src
 * @return  string
 */

function wpse_12448_fallback_jquery( $src, $handle = null ) {
    static $run_next = false;

    if ( $run_next ) {
        // Defaults to WP bundled jQuery, but you should use your own that
        // matches the version loaded via CDN.
        $local = includes_url( 'js/jquery/jquery.js' );

        echo "<script>window.jQuery || document.write( '<script src=\"".$local."\"></script>' );</script>";
        $run_next = false;
    }

    if ( $handle === 'jquery' )
        $run_next = true;

    return $src;
}

add_action( 'wp_head', 'wpse_12448_fallback_jquery', 2 );
if ( ! is_admin() ) {
    add_filter( 'script_loader_src', 'wpse_12448_fallback_jquery', 10, 2 );
}

_____________________________________________________

Comments

Popular posts from this blog

How to fix WordPress 500 Internal Server Errorr after install or upload

When you move a WordPress instalation from one server to another, after you have all files in place, the database, and the database meta updates for the server address you must clear the PERMALINKS . Go to Permalinks -> select some option -> re select old options -> Click save . All pages should be working now . If you still have problems, check folder permissions or user server permissions with the apache service .

Remove Woocommerce Styles & Scripts on other pages

Remove WooCommerce styles and scripts. https://gist.github.com/gregrickaby/2846416   remove-woocommerce-styles-scripts.php <?php //* Do NOT include the opening php tag shown above. Copy the code shown below into functions.php /** * Manage WooCommerce styles and scripts. */ function grd_woocommerce_script_cleaner () { // Remove the generator tag remove_action ( 'wp_head' , array ( $ GLOBALS [ 'woocommerce' ], 'generator' ) ); // Unless we're in the store, remove all the cruft! if ( ! is_woocommerce () && ! is_cart () && ! is_checkout () ) { wp_dequeue_style ( 'woocommerce_frontend_styles' ); wp_dequeue_style ( 'woocommerce-general' ); wp_dequeue_style ( 'woocommerce-layout' ); wp_dequeue_style ( 'woocommerce-smallscreen' ); wp_dequeue_style ( 'woocommerce_fancybox_styles' ); wp_dequeue_style ( 'woocommerce_chosen_styles' ); wp_dequeue...