[wd_asp elements=’search’ ratio=’100%’ id=1]

Defer Javascript until HTML loaded – WordPress – Gtmetrix

18th July 2020

SEO

SEO category

Add this to your functions.php file and it will speed the loading of the page up. It does this by putting .js files to the back of the queue. It helped me by running YouTube plugins last as they were taking ages to load stopping the user viewing the page.
As soon as I added this it was fixed. the page loaded fast and only inserted the last bits after the page was displayed.


function defer_parsing_of_js( $url ) {
if ( is_user_logged_in() ) return $url; //don't break WP Admin
if ( FALSE === strpos( $url, '.js' ) ) return $url;
if ( strpos( $url, 'jquery.js' ) ) return $url;
return str_replace( ' src', ' defer src', $url );
}
add_filter( 'script_loader_tag', 'defer_parsing_of_js', 10 );