Deferring Parsing of Javascript in WordPress

Facebooktwittergoogle_plusredditpinterestlinkedintumblrFacebooktwittergoogle_plusredditpinterestlinkedintumblr

So, I have been configuring WordPress with WooCommerce, and discovering quite a lot as I go. I had optimized the loading speed of the web site by deferring parsing of the javascripts to later in the page, using this code in my child theme functions.php file:

1
2
3
4
5
6
7
8
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 ";
  }
  add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
}

The above code breaks the SyntaxHighlighter Evolved plugin, so I have switched to the WP-Syntax plugin, together with the WP-Syntax Editor Integration plugin. The original code I used to defer loading of javascript broke the WordPress Lightbox functionality. You can read about the code I used at the Uzelac Media website. Note that the code above ‘fixes’ the improper use of apostrophes and quote marks – the code on the linked page, if copied and pasted as is, will not work. Use the code shown above instead.

[Edit: I have since removed this code from my functions.php file and used a plugin called Autoptimize instead.]
Categories: WordPress.