Optimize Core Web Vitals — LCP, CLS, and INP — in WordPress

Optimize Core Web Vitals (LCP, CLS and INP) in WordPress

Core Web Vitals are three user-experience metrics that Google uses in its page experience signals:

  • Largest Contentful Paint (LCP): how fast the largest element above the fold appears. Good: under 2.5 s.
  • Cumulative Layout Shift (CLS): how much the content jumps while loading. Good: under 0.1.
  • Interaction to Next Paint (INP): how fast the page reacts to clicks and taps. Good: under 200 ms. In March 2024 it replaced First Input Delay.

Problem: A WordPress blog scores 45 on PageSpeed Insights mobile: LCP 5.2 s (the hero image loads late), CLS 0.28 (images without dimensions, font swap), INP 380 ms (large scripts in the <head>).

Solution: Give the LCP image priority, keep dimensions on all images and match the fallback font metrics, and load non-critical JavaScript with the defer/async strategies of WordPress 6.3.

LCP: the hero image

The LCP element on a WordPress post is usually the featured image. Since 6.3 WordPress tries to detect it, skips lazy loading for it and adds fetchpriority="high". Check the HTML; if the theme prints the image in a way WordPress does not recognize, set it explicitly:

<?php
add_filter( 'wp_get_attachment_image_attributes', function ( $attr, $attachment, $size ) {
    if ( is_singular() && in_the_loop() && get_post_thumbnail_id() === $attachment->ID ) {
        $attr['fetchpriority'] = 'high';
        $attr['loading']       = 'eager';
    }
    return $attr;
}, 10, 3 );

The rest of LCP is the image itself: the right size from srcset (not a 3000px original), WebP or AVIF, and a fast server response (page cache). A <link rel="preload"> is only needed when the browser discovers the image late, for example a CSS background; an <img> in the HTML with fetchpriority="high" is enough. More on images in lazy loading and the LCP image.

CLS: dimensions and fonts

WordPress adds width and height to content images and to everything printed with wp_get_attachment_image(). Hard-coded <img> tags in templates and images from page builders are the usual offenders. Embeds and ads need a reserved box:

.ad-slot {
    min-height: 250px;
}

.wp-block-embed__wrapper iframe {
    aspect-ratio: 16 / 9;
    width: 100%;
    height: auto;
}

A web font that replaces the fallback font with different metrics shifts the text. A fallback adjusted to the web font’s size reduces the shift:

@font-face {
    font-family: 'MyFont';
    src: url('/wp-content/themes/my-theme/fonts/myfont.woff2') format('woff2');
    font-weight: 400;
    font-display: swap;
}

@font-face {
    font-family: 'MyFont-fallback';
    src: local('Arial');
    size-adjust: 105%;      /* tune these to the metrics of your web font */
    ascent-override: 90%;
    descent-override: 20%;
    line-gap-override: 0%;
}

body {
    font-family: 'MyFont', 'MyFont-fallback', Arial, sans-serif;
}

INP: less JavaScript on the main thread

Slow interactions come from long JavaScript tasks: big bundles parsed at load, heavy handlers on click. WordPress 6.3 added loading strategies to wp_enqueue_script():

<?php
add_action( 'wp_enqueue_scripts', function () {
    // Runs after the HTML is parsed, in order
    wp_enqueue_script( 'mytheme-interactions', get_theme_file_uri( 'js/interactions.min.js' ), array(), '1.0', array(
        'strategy'  => 'defer',
        'in_footer' => true,
    ) );

    // Independent script, runs as soon as it is loaded
    wp_enqueue_script( 'mytheme-analytics', get_theme_file_uri( 'js/analytics.min.js' ), array(), '1.0', array(
        'strategy' => 'async',
    ) );
} );

For scripts of plugins, wp_script_add_data( 'handle', 'strategy', 'defer' ) does the same; see also deferring JavaScript. Beyond loading: break long handlers into smaller tasks, and remove plugins that load scripts on every page for a feature used on one.

NOTE: Lab tools (Lighthouse, the DevTools Performance panel) show the causes, but Google uses field data from real Chrome users (CrUX), shown in Search Console. It is collected over 28 days, so after a fix the report improves only gradually.