On classic and page-builder themes, stop loading the block library stylesheet on pages that contain no blocks.
add_action( 'wp_enqueue_scripts', function (): void {
// Block themes build every page from blocks, so they always need these styles.
if ( wp_is_block_theme() || ! is_singular() ) {
return;
}
$post = get_queried_object();
if ( ! $post instanceof WP_Post || has_blocks( $post->post_content ) ) {
return;
}
wp_dequeue_style( 'wp-block-library' );
wp_dequeue_style( 'wp-block-library-theme' );
wp_dequeue_style( 'classic-theme-styles' );
}, 100 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
WordPress loads the block library stylesheet on every page so blocks look right. Sites built with a page builder such as Bricks, or older posts written in the classic editor, often have pages with no blocks at all. This snippet skips that stylesheet on those pages.
On single posts and pages, has_blocks() checks the content for block markup. If there’s none, the snippet removes wp-block-library, the optional wp-block-library-theme and classic-theme-styles, which only styles blocks on classic themes.
It does nothing at all on block themes. Their headers, footers and templates are made of blocks even when the post content isn’t, so they always need the styles.
The previous version also removed global-styles, the CSS WordPress generates from theme.json. That stylesheet holds colours, fonts and spacing presets your theme and page builder may rely on even without blocks, and removing it could change how the whole page looked.
Open a page built without blocks and view the source: wp-block-library-css should be gone. Open a post written in the block editor and it should still be there.