Remove unused CSS from a WordPress theme with PurgeCSS
A theme stylesheet grows with every component, and every page downloads all of it, also the slider, shop and form styles it never uses. PurgeCSS scans the theme’s templates and scripts for class names and drops the rules that nothing uses. In WordPress there is one trap: many class names are not in the theme files at all, but in the post content in the database.
Problem: The theme’s style.css has 180 KB, most pages use a fraction of it, and Lighthouse reports “Reduce unused CSS”.
Solution: Run PurgeCSS in the theme build over the PHP templates and JavaScript files, keep the classes that come from the editor and from WordPress with a safelist, and split the rest into stylesheets that are enqueued only on the page types that need them.
The PurgeCSS config
npm install --save-dev purgecss
// purgecss.config.js in the theme root
module.exports = {
content: [ './**/*.php', './js/**/*.js' ],
css: [ './css/style.css' ],
output: './dist/',
safelist: {
standard: [ /^wp-/, /^has-/, /^is-/, /^align/, /^screen-reader/, /^menu-/, /^current-/, /^page-/, /^post-/, /^sticky/ ],
greedy: [ /^wp-block-/ ],
},
skippedContentGlobs: [ 'node_modules/**', 'vendor/**' ],
};
npx purgecss --config ./purgecss.config.js
The safelist protects what PurgeCSS cannot see: block classes (wp-block-*, has-*-color, alignwide) stored in the post content, classes added by body_class() and post_class() at runtime, the menu classes from wp_nav_menu(), and state classes such as is-open that scripts build from strings. Check the result on real pages: a missing rule shows up as a broken layout only where the class is used.
Load the parts where they are needed
add_action( 'wp_enqueue_scripts', function () {
$ver = wp_get_theme()->get( 'Version' );
wp_enqueue_style( 'ha-base', get_theme_file_uri( 'dist/base.css' ), [], $ver );
if ( is_home() || is_archive() || is_search() ) {
wp_enqueue_style( 'ha-archive', get_theme_file_uri( 'dist/archive.css' ), [ 'ha-base' ], $ver );
}
if ( is_singular( 'post' ) ) {
wp_enqueue_style( 'ha-single', get_theme_file_uri( 'dist/single.css' ), [ 'ha-base' ], $ver );
}
if ( function_exists( 'is_woocommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() ) ) {
wp_enqueue_style( 'ha-shop', get_theme_file_uri( 'dist/shop.css' ), [ 'ha-base' ], $ver );
}
} );
Each file is purged separately, so a rule that is only in shop.css is never downloaded on a blog post. The theme version in the URL makes browsers fetch the new files after an update.
Core block styles
WordPress itself loads the CSS of all core blocks in one file (wp-block-library). For classic themes, this filter loads only the styles of the blocks on the page, in separate inline parts:
add_filter( 'should_load_separate_core_block_assets', '__return_true' );
Block themes have this behaviour by default since WordPress 5.8.
NOTE: Measure before and after: the transfer size of the CSS (with gzip it is much smaller than on disk) and the render time on a slow connection. If the stylesheet is small after compression, splitting it brings little; the critical CSS approach from deferring JavaScript and CSS may help more.