延迟解析添加到 <link> 标记的 JS

Defer parsing of JS adding to <link> tag

您好,我已将这段代码添加到我的 wp 站点

function defer_parsing_of_js ( $url ) {
    if ( FALSE === strpos( $url, '.js' ) ) return $url;
    if ( strpos( $url, 'jquery.js' ) ) return $url;
    return "$url' defer onload='";
}
add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );

然而它似乎出于某种原因将它添加到此 link 标签中,不确定如何从 link 标签中删除它?

<link rel="stylesheet" id="bfa-font-awesome-css" href="//cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css?ver=4.7.0" defer="" onload="" media="all">

好像找到解决办法了:

function js_defer_attr($tag){
    $scripts_to_exclude = array('.jsdelivr');
    foreach($scripts_to_exclude as $exclude_script){
        if(true == strpos($tag, $exclude_script ) )
        return $tag;
    }
return str_replace( ' src', ' defer="defer" src', $tag );
}
add_filter( 'script_loader_tag', 'js_defer_attr', 10 );