Lazy Load Images and Iframes in WordPress with the Native Loading Attribute and IntersectionObserver

Lazy load images and iframes in WordPress with the native loading attribute

Since WordPress 5.5, images printed through the_content(), the_post_thumbnail() and wp_get_attachment_image() get loading="lazy", and since 5.7 iframes too. The browser then downloads off-screen images only when the visitor scrolls near them, without any JavaScript, which saves a lot of traffic on image-heavy pages. All current browsers support it. Two things decide whether it helps or hurts: the dimensions of the images, and the image at the top of the page.

Problem: A photo-heavy site loads dozens of large images on every page, and the main image at the top (the Largest Contentful Paint element) appears late.

Solution: Let WordPress lazy-load everything below the fold, make sure the hero image is loaded eagerly with fetchpriority="high", and print images with WordPress functions so they have width, height and srcset.

What WordPress does today

WordPress 6.3 (August 2023) made the defaults smarter: it does not add loading="lazy" to the first content images, which are probably above the fold, and it adds fetchpriority="high" to the image it considers the LCP candidate (usually the featured image on a single post). On an up-to-date site, check the HTML of a page first: often there is nothing left to fix.

When the theme needs help

Custom templates sometimes confuse this logic, for example a hero image printed outside the loop. Then mark it explicitly:

<?php
// The hero image at the top of the template
echo wp_get_attachment_image(
    get_post_thumbnail_id(),
    'full',
    false,
    array(
        'loading'       => 'eager',
        'fetchpriority' => 'high',
        'class'         => 'hero__image',
    )
);

and switch off lazy loading for a context where it is wrong:

<?php
add_filter( 'wp_lazy_loading_enabled', function ( $default, $tag_name, $context ) {
    // A theme that prints a full-width image at the top of every archive
    if ( 'img' === $tag_name && 'the_post_thumbnail' === $context && is_home() ) {
        return false;
    }
    return $default;
}, 10, 3 );

The lazy attribute on the LCP image is the most common mistake: the browser waits for layout before it even starts downloading the most important picture.

Width and height

loading="lazy" needs width and height on the image: from them the browser reserves the space before the file arrives, so the content does not jump when it loads (Cumulative Layout Shift). wp_get_attachment_image() adds them, as well as srcset and sizes. In templates, do not hard-code <img src="..."> for Media Library images; with CSS img { max-width: 100%; height: auto; } the attributes do not fix the displayed size.

Embeds

YouTube and Vimeo embeds in the content get loading="lazy" from WordPress too. A YouTube iframe still loads a lot of script once it is near the viewport; for pages with many videos, a preview image that is replaced by the iframe on click (or when it comes close, see the Intersection Observer API) saves much more.

NOTE: Lazy loading only delays downloads; it does not make the files smaller. The biggest savings still come from the right image sizes (add_image_size() and srcset), modern formats (WebP, AVIF) and compression.