WordPress

Lazy Loading in WordPress, Without the Layout Shifts

Lazy loading in WordPress means deferring images and iframes below the fold while keeping above-the-fold assets eager so Largest Contentful Paint stays fast. I implement it with native loading="lazy" on non-critical media, explicit width and height or aspect-ratio to reserve space, and fetchpriority="high" on the hero image.

By Saif Al Jahanwebdev-saif.com

What lazy loading actually changes in WordPress

Lazy loading changes when the browser fetches an image or iframe, not whether it appears. With native lazy loading, the browser waits until an element is near the viewport before requesting it, so a long article or product grid stops competing with the hero image for bandwidth. WordPress core has added loading="lazy" to most content images since 5.5, which means many sites are already deferring more than the developer intended. The real work is deciding which assets should opt out. In my builds the rule is simple: defer anything the visitor cannot see on first paint, and load anything that defines the first screen immediately and at high priority.

  • Defer below-the-fold content images and product thumbnails
  • Keep the LCP image eager and high priority
  • Lazy-load offscreen iframes such as map and video embeds
  • Reserve intrinsic space for every deferred element
  • Check the rendered HTML, not just the theme source

What to lazy-load, and what to keep eager

Start with a rule based on the viewport, not on file size. Hero images, logo marks, above-the-fold product shots and any background image that covers the first screen should load eagerly. Everything below the fold — gallery images, related posts, review thumbnails, footer logos and embedded maps — is a lazy-loading candidate. Carousels need a special case: the first slide is above the fold and must be eager, while the remaining slides can defer. I have seen stores lose LCP time because a theme lazy-loaded the first product image, and I have seen pages fetch dozens of offscreen thumbnails because nobody deferred them at all.

  • Eager: hero image, logo, first carousel slide, LCP background
  • Lazy: galleries, related posts, grids, offscreen avatars
  • Lazy: YouTube, Vimeo and Google Maps iframes
  • Never lazy-load an image just because the file is small
  • Treat CSS background images separately from img tags

Native loading, decoding and fetchpriority hints

Three attributes control image behaviour in the browser, and they do different jobs. loading="lazy" defers the request; loading="eager" requests it immediately. decoding="async" lets the browser decode off the main thread, which helps long image lists but should not be applied blindly to the hero image — decoding="sync" can be preferable for the LCP candidate because that image is needed at once. fetchpriority="high" tells the browser the hero matters more than the other assets in the same viewport, while fetchpriority="low" suits offscreen thumbnails that are going to be requested anyway. These hints are free to add and show up in the rendered markup, so they are easy to verify in DevTools.

  • loading="lazy" on offscreen img and iframe elements
  • decoding="async" for galleries and long product grids
  • fetchpriority="high" on the single LCP image only
  • fetchpriority="low" for non-critical above-the-fold thumbnails
  • One high-priority image per page — more just creates a queue

Reserving space: the CLS side of lazy loading

Lazy loading without reserved space is a direct route to a high Cumulative Layout Shift score. When the browser defers an image and the surrounding container has no height, text and buttons sit at the top until the image arrives and push everything down. The fix is to give every image an intrinsic size before it loads. WordPress usually writes width and height attributes from attachment metadata through wp_get_attachment_image(), and CSS aspect-ratio on the wrapper handles cases where attributes are stripped. For iframes, set a fixed height or a padding-based aspect-ratio box. Avoid content-visibility: auto unless you also set contain-intrinsic-size, or you will trade one shift for another.

  • Explicit width and height on every content image
  • aspect-ratio on wrappers when attributes are unavailable
  • Fixed-size or aspect-ratio containers for iframes
  • contain-intrinsic-size when using content-visibility: auto
  • Target CLS below 0.1 on mobile, measured in the field

Embeds, backgrounds and the cases core does not cover

Core lazy loading covers img tags and, in recent versions, iframes, but it will not touch CSS background images or JavaScript-injected markup. For a video embed I prefer a lightweight facade: render a poster image with the correct dimensions, load the real YouTube or Vimeo iframe only after a click, and the visitor never downloads the player script on page load. For background images that are decorative or below the fold, load them with a small script that swaps a data-src attribute when the element intersects the viewport using IntersectionObserver with a rootMargin. Reserve the box in CSS so the swap causes no shift. Let the browser handle the common path; write custom code only where native behaviour stops.

  • Poster-and-click facade for video embeds
  • IntersectionObserver with rootMargin for background images
  • Keep the placeholder box the same size as the final asset
  • Exclude scripts and fonts from do-it-yourself lazy loading

How I wire it into a WordPress theme

I am Saif Al Jahan, a WordPress and PHP developer in Mymensingh, Bangladesh, and in my themes I let core do the default work and override only the exceptions. wp_get_attachment_image() and the_content already emit loading="lazy" and decoding="async"; I hook wp_img_tag_add_loading_attr and wp_get_attachment_image_attributes to force loading="eager", decoding="sync" and fetchpriority="high" on the hero selected by the template. srcset and sizes still come from wp_get_attachment_image_srcset(), so responsive images keep working — lazy loading must never replace a good sizes attribute. For product grids I add fetchpriority="low" to later thumbnails. I document the exceptions in the theme README, because the next developer should not have to guess why one image is treated differently.

  • wp_img_tag_add_loading_attr to override the loading attribute
  • wp_get_attachment_image_attributes for attachment-level changes
  • Keep srcset and sizes generated by WordPress
  • Document hero exceptions in the theme README

Measure LCP, CLS and INP before and after

The only useful verdict comes from field data, not a single Lighthouse run. I record Largest Contentful Paint, Cumulative Layout Shift and Interaction to Next Paint before changing lazy-loading rules, then compare after deploy under the same conditions: mid-range Android, throttled 4G, cold cache. A practical target is LCP under 2.5 seconds, CLS under 0.1 and INP under 200 milliseconds at the 75th percentile. If LCP gets worse after enabling lazy loading, the hero is almost always being deferred. If CLS gets worse, a deferred element has no reserved box. The Performance panel in Chrome DevTools and the Web Vitals extension are enough to confirm both.

Frequently asked questions

Should I lazy-load every image in WordPress?

No. Lazy-load images the visitor cannot see on the first screen, and keep the hero, logo and first carousel slide eager. WordPress already adds loading="lazy" to most content images, so the common problem is too much deferral, not too little. Check the rendered HTML and force the LCP image to load eagerly with high priority.

Does lazy loading hurt LCP?

Yes, when it is applied to the largest above-the-fold image. The browser holds the request until it decides the image is near the viewport, which delays the paint that LCP measures. Keep that image eager and add fetchpriority="high" to it. Deferring offscreen images usually improves LCP because it frees bandwidth for the hero.

What does the decoding="async" hint do?

It tells the browser it may decode the image off the main thread, leaving more room for input handling and layout work. I use decoding="async" on galleries and long product grids where many images arrive together. For the single LCP image I prefer decoding="sync", because that image is needed immediately and an extra scheduling step can work against you.

How do I stop lazy-loaded images from causing layout shift?

Reserve the space before the image arrives. Set explicit width and height attributes from WordPress attachment metadata, or give the wrapper a CSS aspect-ratio, and do the same for iframes with a fixed or ratio-based box. If you use content-visibility: auto, also set contain-intrinsic-size. These boxes keep text and buttons from moving when the deferred asset finally paints.

Do I still need a lazy-load plugin in WordPress?

Usually no. WordPress core has emitted loading="lazy" on content images since 5.5, and modern browsers handle img and iframe deferral natively. A plugin earns its place only for gaps such as CSS background images, JavaScript embeds or a click-to-load video facade. Before installing one, inspect the page source: most sites need a handful of filter overrides in the theme, not another plugin.

Should lazy loading be used for WooCommerce product images?

Yes for grids and galleries, no for the first above-the-fold product photo. Archive pages can load dozens of thumbnails, so deferring the ones below the fold cuts initial requests noticeably. Keep srcset and sizes intact so each visitor still gets the right file for their screen, and use fetchpriority="low" on later thumbnails rather than removing them entirely.

Written by Saif Al Jahan, webdev-saif.com · Mymensingh, Bangladesh. Last updated .