PageSpeed Insights reports a 4.2s LCP and “properly size images” as the top opportunity. The hero image is 4096px wide rendered into a 1280px container. WordPress has emitted srcset since 4.4 and loading="lazy" since 5.5 — yet here we are. Themes, page builders, and well-intentioned performance plugins routinely strip or override both attributes. Auditing what the browser actually receives is the only way to find out which layer broke them.
What’s happening
Three browser features interact:
Native lazy loading (<img loading="lazy">) defers image fetch until the element approaches the viewport. WordPress adds it by default to images below the first viewport via wp_lazy_loading_enabled() and wp_filter_content_tags(). The filter explicitly skips the first image on a page (intended LCP candidate). Themes that build their hero by directly outputting <img> from a custom field — instead of through wp_get_attachment_image() — bypass this filter entirely, and either get no lazy loading or get loading="lazy" on the LCP image (the worst outcome).
srcset + sizes lets the browser pick the right resolution. WordPress emits both via wp_calculate_image_srcset() and wp_calculate_image_sizes(). The sizes attribute defaults to (max-width: $w) 100vw, $w which assumes the image fills the viewport up to its intrinsic width — wrong for any image rendered into a column. The browser picks the largest candidate that fits the viewport; with a wrong sizes value, it consistently overshoots.
fetchpriority="high" added in 2023, hints to the browser that an image should be downloaded early. Critical for LCP. WordPress 6.3+ auto-applies it to the first large image on a page (wp_get_loading_optimization_attributes). Themes that emit <img> directly miss this too.
The common misfires:
- Theme strips
srcset. A page builder regenerates<img>tags after the filter pass, droppingsrcset. Browsers always download the full-size attachment. sizesis wrong. Theme containers cap at 760px butsizessays100vw. On a 1440px viewport the browser pulls a 1536px image where 760px would do.- LCP image is lazy-loaded. Page builder or block override marks the hero
loading="lazy". Browser delays the most important fetch. fetchpriority="high"is missing from the LCP image because it’s rendered through a custom field, ACF, or a Pods loop that doesn’t go throughwp_get_attachment_image.<picture>substitution interferes. A WebP plugin wraps<img>in<picture>but copies the wrong attributes —loading="lazy"survives butsrcsetis dropped from the<source>tag.
Core Web Vitals impact is real: a correctly-sized hero with fetchpriority="high" and no lazy load typically saves 600-1200ms LCP on a content-heavy page over the same hero with default WordPress output.
How to verify
Three checks. First, what WordPress is emitting:
# Pull the rendered HTML and inspect img attributes
curl -sL https://example.com/ | \
grep -oE '<img [^>]*>' | head -20
# Extract loading/fetchpriority/srcset/sizes summary
curl -sL https://example.com/ | python3 -c '
import sys, re
html = sys.stdin.read()
for m in re.finditer(r"<img\s+([^>]+)>", html):
attrs = dict(re.findall(r"(\w+)=[\x22]([^\x22]*)[\x22]", m.group(1)))
print(f"loading={attrs.get(\"loading\",\"-\"):>5} fetchpriority={attrs.get(\"fetchpriority\",\"-\"):>4} srcset={\"yes\" if \"srcset\" in attrs else \"no\"} sizes={attrs.get(\"sizes\",\"-\")[:40]}")
' | head -20
Second, what the browser actually fetches. Open Chrome DevTools → Network → filter by Img. Reload with cache disabled. Look at:
- The LCP image: is it large enough to matter? Is
Priority: High? - The first 10 images: should be download priority Low except hero. If you see five images with
Priority: High, lazy loading is broken. - Resource size vs displayed size: in the Elements panel, hover an image to see
intrinsic vs rendered. A 3000x2000 image rendered at 760x507 is ~75% wasted bytes.
Third, audit programmatically via WP-CLI on a known page:
# Inspect what filters are running on the_content
wp eval '
add_filter("the_content", function($c) {
error_log("the_content output: " . substr($c, 0, 1000));
return $c;
}, PHP_INT_MAX);
echo apply_filters("the_content", get_post(123)->post_content);
' --skip-themes=false
If loading= is missing from <img> tags in the dumped content, either the theme is bypassing core filters, a plugin is post-processing the HTML, or wp_filter_content_tags() is being disabled somewhere.
The fix
Five interventions, in order of typical impact:
1. Remove loading="lazy" from the LCP image. WordPress core does this if the image is rendered through wp_get_attachment_image() and is in the first viewport. For themes that build their hero from custom fields, add an explicit override:
// theme/functions.php
add_filter('wp_get_attachment_image_attributes', function($attr, $attachment, $size) {
static $first = true;
if ($first && is_singular() && in_the_loop() && is_main_query()) {
unset($attr['loading']);
$attr['fetchpriority'] = 'high';
$first = false;
}
return $attr;
}, 10, 3);
Better: feed the LCP image through wp_get_attachment_image() instead of a custom output. Let core handle it.
2. Fix sizes to match the actual layout. If your content area is max-width: 760px, override the default 100vw:
add_filter('wp_calculate_image_sizes', function($sizes, $size, $image_src) {
if (is_singular() && in_the_loop()) {
return '(max-width: 760px) 100vw, 760px';
}
return $sizes;
}, 10, 3);
For a multi-column layout where images render into different widths depending on viewport, build the sizes value to match:
<img sizes="(max-width: 600px) 100vw, (max-width: 1024px) 50vw, 760px" srcset="...">
3. Verify srcset survives every filter. If a page builder or plugin strips srcset, re-emit it via the_content:
add_filter('the_content', function($content) {
// Restore srcset/sizes for img tags that have an attachment ID hint
return preg_replace_callback(
'/<img([^>]+)class="[^"]*wp-image-(\d+)[^"]*"([^>]*)>/i',
function($m) {
$id = (int)$m[2];
if (strpos($m[0], 'srcset=') !== false) return $m[0];
$meta = wp_get_attachment_metadata($id);
if (!$meta) return $m[0];
$srcset = wp_calculate_image_srcset(
[$meta['width'], $meta['height']],
wp_get_attachment_url($id), $meta, $id
);
return $srcset
? str_replace('<img', '<img srcset="' . esc_attr($srcset) . '"', $m[0])
: $m[0];
},
$content
);
}, 999);
4. Picture tag handling for WebP/AVIF. If you’re using <picture> substitution (see the WebP/AVIF pipeline article), make sure srcset survives into the <source> tag too:
add_filter('wp_get_attachment_image', function($html, $attachment_id, $size) {
if (!preg_match('/srcset="([^"]+)"/', $html, $srcset_match)) return $html;
$srcset = $srcset_match[1];
// Translate JPG/PNG paths to WebP in srcset
$webp_srcset = preg_replace_callback('|(\S+)\.(jpe?g|png)|i', function($m) {
return $m[1] . '.webp';
}, $srcset);
$sources = '<source type="image/webp" srcset="' . esc_attr($webp_srcset) . '">';
return '<picture>' . $sources . $html . '</picture>';
}, 20, 3);
5. Block-builder safety net. For sites using Gutenberg core blocks: the core/image block respects all the right attributes if you set them in the block sidebar. The hidden trap is custom blocks built before WP 6.3 that hard-code loading="lazy". Audit your block.json templates and remove any loading default that overrides core’s filter.
After every fix, re-run the audit. The browser’s network panel is the source of truth — not the HTML you think you emitted.
Stack Harbor audits Core Web Vitals as part of Enterprise WordPress, including the theme + plugin interactions that quietly undo image optimization. For sites where Lighthouse keeps regressing every release, book a performance audit.