WordPress:在 functions.php 中添加 JavaScript 的延迟解析时未加载古腾堡
WordPress: Gutenberg not loading when Defer Parsing of JavaScript added in functions.php
我正在尝试将 JavaScript 函数的延迟解析合并到我的 functions.php 文件中。当我这样做时,网站仍然运行良好,但在 wp-admin 中,任何涉及古腾堡的东西都不会加载,我只会看到白屏死机。知道这段代码是否不好吗?
// 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 );
您可能下载此代码的页面中有解决该问题的评论。改用这个:
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 );
来源:https://kinsta.com/blog/defer-parsing-of-javascript/(院长回答)
我正在尝试将 JavaScript 函数的延迟解析合并到我的 functions.php 文件中。当我这样做时,网站仍然运行良好,但在 wp-admin 中,任何涉及古腾堡的东西都不会加载,我只会看到白屏死机。知道这段代码是否不好吗?
// 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 );
您可能下载此代码的页面中有解决该问题的评论。改用这个:
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 );
来源:https://kinsta.com/blog/defer-parsing-of-javascript/(院长回答)