Skip to content

WordPress critical CSS: a practical implementation that survives templates

WordPress critical CSS done without manual per-page work — automated generation, in-template inlining, fallback for templates the generator missed, and the maintenance cycle that keeps it correct.

Critical CSS is the optimization that everyone talks about and few WordPress sites have working correctly. A site with critical CSS in place loads its first viewport without waiting for the full stylesheet. A site that “has critical CSS enabled” in WP Rocket but never regenerates it ships stale styles for six months and looks broken on every new template.

What’s happening

Critical CSS is the subset of CSS rules required to render the above-the-fold portion of a page. Inlined in the document head, it lets the browser paint the first viewport without waiting for the main stylesheet to download. The rest of the CSS loads asynchronously and styles the rest of the page when it arrives.

The generation problem: critical CSS depends on the rendered HTML, which depends on the WordPress template, which depends on the URL. A homepage critical CSS is not the same as a blog post critical CSS. A WooCommerce product critical CSS is not the same as a cart critical CSS. A correct implementation generates critical CSS per template, not per site, and re-generates when the template changes.

How to verify

Inspect the current state of CSS loading:

curl -s https://www.example.com/ \
  | grep -oE '<link[^>]+stylesheet[^>]*>|<style[^>]*>[^<]{0,200}' | head

A site with critical CSS will have a <style> block in the head containing 5-15 KB of inlined CSS, followed by <link rel="preload" href="/wp-content/.../style.css" as="style" onload="this.rel='stylesheet'">. A site without critical CSS will have a plain <link rel="stylesheet"> blocking render.

Measure the impact:

# Render-blocking CSS in Lighthouse
lighthouse https://www.example.com/ --output=json \
  | jq '.audits["render-blocking-resources"], .audits["unminified-css"]'

The “render-blocking-resources” audit lists every stylesheet that delayed the first paint. Critical CSS done correctly removes the main stylesheet from this list.

The fix

  1. Pick a generator that runs per template: WP Rocket’s “Optimize CSS delivery” generates critical CSS but treats the site as one URL. Better: use Critical (the open-source CLI by Addy Osmani and contributors), penthouse, or a paid SaaS like CriticalCSS.com. Generate one critical CSS file per template (home, single, archive, page, product, cart, checkout).
  2. Inline the right file per template: in the WordPress theme, use get_template_part logic or the body_class to determine the current template and inline the matching critical CSS file:
add_action('wp_head', function() {
    if (is_front_page()) $file = 'critical-home.css';
    elseif (is_singular('post')) $file = 'critical-single.css';
    elseif (is_singular('product')) $file = 'critical-product.css';
    elseif (is_cart() || is_checkout()) $file = 'critical-cart.css';
    else $file = 'critical-default.css';

    $path = get_stylesheet_directory() . '/critical/' . $file;
    if (file_exists($path)) {
        echo '<style id="critical-css">' . file_get_contents($path) . '</style>';
    }
}, 1);
  1. Defer the main stylesheet: change the standard <link rel="stylesheet"> to a preload-and-swap pattern using wp_enqueue_style with a filter on style_loader_tag.
  2. Cover the templates the generator missed: a custom template (page-landing.php), a third-party block (Yoast schema renderer), or a logged-in-only template will not have critical CSS generated. Fall back to the default critical CSS file rather than inlining nothing.
  3. Regenerate critical CSS on deploy: bake critical-CSS regeneration into the deploy pipeline. After a theme change, the previous critical CSS may reference removed classes or miss new ones. Regenerate as part of the build, not as an on-demand WordPress task.
  4. Test for FOUC and missing-style regressions: a critical CSS file missing one class produces visible flashes of unstyled content. Run a Lighthouse-or-Percy visual regression on key templates before promoting.

Re-measure render-blocking time after deployment. The expected LCP improvement is 300-700 ms on a render-blocking heavy WordPress theme.

Stack Harbor maintains critical CSS pipelines as part of the deploy workflow on Enterprise WordPress accounts. If your site has critical CSS configured but the render-blocking audit still flags the main stylesheet, book a 30-minute review.