这里的游戏有点晚了,但为了添加到@Rilwis answer,您可以将脚本添加到一个函数中,该函数检查no-js 是否添加语言属性。
虽然我没有使用Modernizr,但我确实使用了2016年的no-js检测脚本,因此包含内容基本相同。这样做,您就不必担心删除添加到wp_head 或wp_enqueue_scripts 因为当你移除过滤器时,它会自动发生。
添加删除筛选器方法
/** 
 * Javascript Detection Filter
 * 
 * remove_filter( \'language_attributes\', \'lang_atts_js_detect\' ); 
 */ 
add_filter( \'language_attributes\', \'lang_atts_js_detect\' );
function lang_atts_js_detect($output) {
    return $output . \' class="no-js"\';
}
add_action( \'wp_head\', function() {
    if ( has_filter( \'language_attributes\', \'lang_atts_js_detect\' ) ) {
        echo "<script>(function(html){html.className = html.className.replace(/\\bno-js\\b/,\'js\')})(document.documentElement);</script>\\n";
    }
}, 0);
 另一种选择是使用
current_theme_supports.
/** 
 * Javascript Detection Theme Support
 * 
 * add_theme_support( \'js-detect\' ); 
 */ 
add_action( \'init\', function() {
    if ( current_theme_supports( \'js-detect\' ) && !is_admin() )
        add_filter( \'language_attributes\', \'lang_atts_js_detect_class\' );
        add_action( \'wp_head\', \'lang_atts_js_detect_script\', 0 );
}
function lang_atts_js_detect_class($output) {
    return $output . \' class="no-js"\';
}
function lang_atts_js_detect_script() {
    echo "<script>(function(html){html.className = html.className.replace(/\\bno-js\\b/,\'js\')})(document.documentElement);</script>\\n";
}