I'm trying to incorporate a Defer Parsing of JavaScript function into my functions.php file. When I do, the site still works fine, but in wp-admin, anything involving Gutenberg wont' load and I just get the white screen of death. Any idea if this code is bad?
// Defer Parsing of Javascript
function defer_parsing_of_js ( $url ) {
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 );
The page where you probably downloaded this code has a comment in it that solved it. Use this instead:
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 );
Source: https://kinsta.com/blog/defer-parsing-of-javascript/ (Answer by Dean)