新旧コンテンツ混在
長いこと運営している WordPress サイトにおいては、
- ブロックエディターで書かれているコンテンツ (比較的最近の投稿)
- クラシックエディターで書かれているコンテンツ (古い投稿)
が 混在しているケースも少なくないかなと思います。
※ ブロックエディターが導入されたのは、2018年12月の WordPress 5.0 から
global-styles-inline-css
WP5.9以降、<head> 内に <style id=”global-styles-inline-css”> ~ </style> というCSSがインラインで挿入されるようになりました。 (結構なボリュームです)
ブロックエディターで書かれていないページでは不要となる、このインラインCSSを選択的に除外するコードの覚書を記します。
functions.php に
PHP
// <style id="global-styles-inline-css"> を選択的に除外
add_action( 'wp', function () {
if ( ! is_singular() ) return;
$post = get_post();
if ( ! $post ) return;
if ( ! has_blocks( $post ) ) { // ブロックエディターで書かれていない場合に除外
remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
remove_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );
}
} );を追記することで、global-styles-inline-css の出力を必要なページのみに限定できます🙂





