• Hi Everyone,

    I was trying to change the static JQuery File to using CDN. I have inserted these lines of code and I researched a lot before inserting this code:

    add_action( 'wp_enqueue_scripts', 'jquery_bind' );
    function jquery_bind() {
    // instead of "jquery-core" just "jquery", to disable jquery-migrate
    wp_deregister_script( 'jquery-core-js' );
    wp_register_script( 'jquery-core', 'https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js',false);
    wp_enqueue_script( 'jquery' );
    }
    
    add_action( 'wp_enqueue_scripts', 'jquery_migrate_bind' );
    function jquery_migrate_bind() {
    // instead of "jquery-core" just "jquery", to disable jquery-migrate
    wp_deregister_script( 'jquery-migrate-js' );
    wp_register_script( 'jquery-core', 'https://cdnjs.cloudflare.com/ajax/libs/jquery-migrate/3.4.0/jquery-migrate.min.js',false);
    wp_enqueue_script( 'jquery' );
    }

    The problem is still the same and now my console is full of errors. My head tag still has the <script> tag for the static jquery file on my server. Can anyone tell me how can I use the CDN with my WordPress website?

    Console Error:

    Failed to load resource: the server responded with a status of 404 ()
    (jquery.min.js:1)

    Failed to load resource: the server responded with a status of 404 ()
    (jquery-migrate.min.js:1)

    I am using WP Super Cache.

    Thanks,
    Kavyesh

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • You can’t do that the way you think. Here is a way that works:
    https://gist.github.com/Shelob9/de011337264ed365c52dc1561746ec12

    However, you still have to adapt the URL of the migration script. I have successfully tested the following for myself:

    add_action( 'init', function(){
        if ( ! is_admin()) {
            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->src = "$protocol://ajax.googleapis.com/ajax/libs/jquery/$core_version/jquery.min.js";
    
            if ( WP_DEBUG ) {
                /** @var _WP_Dependency $migrate */
                $migrate = $wp_scripts->registered[ 'jquery-migrate' ];
                $migrate_version = $migrate->ver;
                $migrate->src = "$protocol://cdnjs.cloudflare.com/ajax/libs/jquery-migrate/$migrate_version/jquery-migrate.min.js";
            }else{
                /** @var _WP_Dependency $jquery */
                $jquery = $wp_scripts->registered[ 'jquery' ];
                $jquery->deps = [ 'jquery-core' ];
            }
        }
    },11 );

    However, I also have to say that I don’t think much of it. For me, outsourcing files to CDN is not a real solution to achieve anything. If it’s speed optimisation you’re after, look at other things like asynchronous loading.

Viewing 1 replies (of 1 total)
  • The topic ‘Not able to integrate Jquery CDN with my website’ is closed to new replies.