0

I use Cloudflare Rocket Loader to speed up Javascript on my site.

The problem is, it loads certain scripts that need jQuery before loading jQuery itself making them not working correctly.

As soon as it is possible to disable Rocket Loader for specific script adding data-cfasync="false" to the <script src=""> I would like to know how can I add it to jQuery scripts as Wordpress doesn't simply load the jQuery via the script tag I guess.

At the moment my website loads jquery-migrate.min.js and jquery.js

PS there is a similar question but it's almost 7yrs ago so, it may be outdated at this point Cloudflare's Rocket Loader + Wordpress -> Ignore scripts?

1

jQuery is loaded by WordPress with wp_enqueue_script. The problem is Wordpress also use the below code to change the handle

public function localize( $handle, $object_name, $l10n ) {
    if ( 'jquery' === $handle ) {
        $handle = 'jquery-core';
    }

The solution is to use the function

function wpse_script_loader_tag( $tag, $handle ) {
    if ( 'jquery-core' !== $handle ) {
        return $tag;
    }

    return str_replace( ' src', ' data-cfasync="false" src', $tag );
}
add_filter( 'script_loader_tag', 'wpse_script_loader_tag', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.