Defer parsing of Javascript

Hey,

I found the following snippet in another post here and it seems to work pretty good on the front end of a site I'm putting together. Unfortunately, it is breaking the editor on the backend of the site. Can anyone suggest a way to get this to apply to the frontend but not the backend?

//Defer Parsing JS
function defer_parsing_of_js ( $url ) {
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
return "$url' defer ";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
  • Tyler Postle
    • CGO

    Hey Pixel,

    Thanks for posting that :slight_smile:

    Any specific reason you are wanting to manually do this vs the Autoptimize plugin? https://wordpress.org/plugins/autoptimize/

    You likely saw it on that thread already. It will defer parsing of Js among other optimizations. Looks to be updated frequently and have a high rating as well :slight_smile: or does that break the backend too?

    Look forward to hearing back.

    All the best,
    Tyler

  • Jason
    • New Recruit

    Yeah I tried Autoptimize but it didn't get on very well with my site at all. It didn't break the editor funny enough but it did mess around with a lot of front end styling and functionality.

    I've managed to find non-plugin methods to achieve most of the optimisation of the site but I would like to crack the JavaScript optimisation too.

  • Ash
    • WordPress Hacker

    Hello @pixelboxdesignuk

    Try this:

    //Defer Parsing JS
    function defer_parsing_of_js ( $url ) {
    if( ! WP_ADMIN ) {
    if ( FALSE === strpos( $url, '.js' ) ) return $url;
    if ( strpos( $url, 'jquery.js' ) ) return $url;
    return "$url' defer ";
    }
    }
    add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );

    or

    //Defer Parsing JS
    function defer_parsing_of_js ( $url ) {
    if( ! defined( 'WP_ADMIN' ) ) {
    if ( FALSE === strpos( $url, '.js' ) ) return $url;
    if ( strpos( $url, 'jquery.js' ) ) return $url;
    return "$url' defer ";
    }
    }
    add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Jason
    • New Recruit

    Hey Ash,

    Cheers for that. Unfortunately they both had a whacky effect on my Admin menu where all the menu links pointed back to the main admin menu. It was amusing but not particularly practical.

    Thanks again,

    Jason.

  • Ash
    • WordPress Hacker

    Hello @Jason

    How's about these?

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

    or

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

    Then check this site as a normal user.

    Cheers
    Ash

  • Ash
    • WordPress Hacker

    I am sorry @Jason in that case I am not getting any more idea :slight_frown:

    The initial code worked for me, but not sure why it didn't work for you. If you want I can try to do some changes in your site. In that case, you need to provide ftp and admin login.

    Please let us know.

    Cheers
    Ash

  • Jason
    • New Recruit

    No worries Ash, your help is very much appreciated anyway. I guess our selection of theme/plugins just make deferring js impossible. We're still on our local server without external access so I can't set you up on there but thanks anyway.

    Jason.

  • Jason
    • New Recruit

    Hey Michelle,

    Thanks for that. deferring the JS was really just dotting the t's and crossing the i's so I'm not majorly concerned that we haven't managed to do it. It definitely wouldn't be something I'd remove or swap other plugins for.

    Cheers for the offer though, much appreciated.

    Jason

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.