Performance PHP Beginner

Load Block Styles Only for Blocks on the Page

On classic themes, load each block's CSS only when that block is used, instead of one big stylesheet for every block.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 1 lines
add_filter( 'should_load_separate_core_block_assets', '__return_true' );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

On classic themes, WordPress loads the full block library stylesheet on every page, covering every block whether you use it or not. Block themes already load styles per block. This one line gives classic themes the same behaviour.

How it works

When should_load_separate_core_block_assets returns true, WordPress swaps the combined wp-block-library stylesheet for small per-block stylesheets, and only loads the ones for blocks that render on the page. Many are small enough that WordPress prints them inline.

Things to watch

  • Blocks rendered after the page head is printed, such as in some page builder templates, still get their styles, but in the footer. If you see a flash of unstyled blocks, remove the snippet.
  • If you use a page builder and no blocks at all, the “Skip Block CSS on Pages Without Blocks” snippet removes the styles completely instead.

How to test it

View the source of a simple post. Instead of one wp-block-library-css file you should see small styles such as wp-block-paragraph and wp-block-heading.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only