Support » Developing with WordPress » How can I defer these JS files?

  • webofpeace


    In my website, I used GTmetrix and I got JS defer error on following two files.

    92.5KiB of JavaScript is parsed during initial page load. Defer parsing JavaScript to reduce blocking of page rendering.

    http://mysite.url/wp-includes/js/jquery/jquery.js?ver=1.12.4 (87.0KiB)

    http://mysite.url/ (5.5KiB of inline JavaScript)

    In my functions.php file I have following code to defer js but it seems not affected for js file(s) outside the theme folder.

    Here’s my piece of code in functions.php file

    if (!(is_admin() )) {
        function defer_parsing_of_js ( $url ) {
            if ( FALSE === strpos( $url, '.js' ) ) return $url;
            if ( strpos( $url, 'jquery.js' ) ) return $url;
            // return "$url' defer ";
            return "$url' defer onload='";
        add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );

    How can I get rid of that error and defer parsing above mentioned items?

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @webofpeace,

    There appears to be a good plugin that provides this ability:

    There’s also an overarching optimization plugin by the same author:

    I don’t have personal experience with either of these, but perhaps they will provide what you are looking for.




    Hi @bpayton,
    Thank you fir the reply. but I want to do this without using any plugin. do you know any way to achieve the expected outcome in that way?without using any 3rd party plugins.


    I’ve checked both plugins and I found this on autoptimize’s FAQs.

    Why is jquery.js not optimized
    Starting from AO 2.1 WordPress core’s jquery.js is not optimized for the simple reason a lot of popular plugins inject inline JS that is not aggregated either (due to possible cache size issues with unique code in inline JS) which relies on jquery being available, so excluding jquery.js ensures that most sites will work out of the box. If you want optimize jquery as well, you can remove it from the JS optimization exclusion-list (you might have to enable “also aggregate inline JS” as well or switch to “force JS in head”).

    so it wont defer the files I want, sadly 🙁

    • This reply was modified 5 months ago by  webofpeace.
    • This reply was modified 5 months ago by  webofpeace.
    • This reply was modified 5 months ago by  bcworkz. Reason: fixed formatting glitch
    Moderator bcworkz


    The problem with your solution is the “clean_url” filter is not universally applied. The right approach depends on how jQuery is included to begin with. Everyone should in theory be enqueuing it. In that case, deregister “jquery” and re-register again, this time setting the $in_footer argument to true. All other scripts dependent on jQuery must then also be registered/enqueued with $in_footer set.

    If the WP enqueue system is being bypassed for some reason, find the code responsible for outputting the jQuery reference and add “async” attribute in the related script tag.

    Another alternative FWIW:

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.