简体   繁体   中英

WordPress: Gutenberg not loading when Defer Parsing of JavaScript added in functions.php

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM