Код: Выделить всё
function defer_css( $html, $handle ) {
$handles = array( 'some_id' );
if ( in_array( $handle, $handles ) ) {
$html = str_replace( 'media=\'all\'', 'media=\'print\' onload="this.onload=null;this.media=\'all\'"', $html );
}
return $html;
}
add_filter( 'style_loader_tag', 'defer_css', 10, 2 );
< /code>
Это, конечно, отвергается моим CSP. Чтобы избежать этого, я стараюсь внедрить ссылку со сценарием, который имеет атрибут Nonce (идентичный тому в заголовке): < /p>
function x_style_loader_tag($html, $handle, $href) {
$async_loading = array(
'some_id'
);
if (in_array($handle, $async_loading)) {
$nonce = apply_filters("TOA_PLUGIN/nonce_scriptx", NULL);
$script = ''.
//printing nonce to the console to see if it matches the csp header, which is true
'console.log(\''.$nonce.'\'); '.
'let s = document.createElement(\'link\'); '.
//'s.id = "'.$handle.'"; '.
's.rel = "stylesheet"; '.
's.href = "'.$href.'"; '.
's.media = "print"; '.
//the following line does not swap the media attribute to all
's.onload = "this.onload=null;this.media=\'all\'"; '.
'document.head.appendChild(s); '.
'';
$html = $script;
}
return $html;
}
add_filter('style_loader_tag', 'x_style_loader_tag', 10, 3);
Код: Выделить всё
Код: Выделить всё
Подробнее здесь: https://stackoverflow.com/questions/796 ... -working-o