Title: Refining WordPress core’s lazy-loading implementation
Author: Felix Arntz
Published: July 15, 2021
Last modified: July 16, 2021

---

 [  ](https://profiles.wordpress.org/flixos90/) [Felix Arntz](https://profiles.wordpress.org/flixos90/)
11:58 pm _on_ July 15, 2021     
Tags: [feature-lazyloading ( 8 )](https://make.wordpress.org/core/tag/feature-lazyloading/)

# Refining WordPress core’s lazy-loading implementation

Since the [image lazy-loading feature was originally added to WordPress core in 5.5](https://make.wordpress.org/core/2020/07/14/lazy-loading-images-in-5-5/),
by default every post content image as well as every image rendering using the [`wp_get_attachment_image()` function](https://developer.wordpress.org/reference/functions/wp_get_attachment_image/)(
which includes featured images) is lazy-loaded. This optimization follows the typical
implementation of the feature in other projects, without consideration for whether
an image appears above or below the fold. Due to the available metrics at the time
it was decided to leave fine-grained control over opting out certain images from
being lazy-loaded to theme authors, since the position of images heavily depends
on the current theme layout. See also the following paragraph in the [original announcement post](https://make.wordpress.org/core/2020/07/14/lazy-loading-images-in-5-5/):

> Theme developers are recommended to granularly handle `loading` attributes for
> images anytime they rely on `wp_get_attachment_image()` or another function based
> on it (such as `the_post_thumbnail()` or `get_custom_logo()`), depending on where
> they are used within templates. For example, if an image is placed within the `
> header.php` template and is very likely to be in the initial viewport, it is advisable
> to skip the `loading` attribute for that image.

It was recently [discovered that the current WordPress core implementation of lazy-loading has room for improvement](https://web.dev/lcp-lazy-loading/)
as it can regress the [Largest Contentful Paint metric (LCP)](https://web.dev/lcp/)
when hero images above the fold are being lazy-loaded. Therefore, the default behavior
of the existing lazy-loading implementation should be refined in order to better
take this nuance into account.

## **Omitting the `loading` attribute on certain elements**

As mentioned before, WordPress coreCore Core is the set of software required to 
run WordPress. The Core Development Team builds WordPress. is unable to reliably
assess whether an image is going to appear above or below the fold, since it depends
on the currently active theme. Furthermore, the `loading` attributes are added on
the server-side, which has no notion on the influencing client factors like viewport
size. Because of those reasons, fine-grained control over lazy-loading images (and
iframes) still remains with theme authors. However, we _can_ fine tune WordPress
core’s lazy-loading implementation to behave in a way that improves the situation
for the vast majority of cases, to achieve better LCP values out of the box.

Now, one could argue that lazy-loading should be entirely removed again to avoid
any negative impact on LCP. However, that would also remove all the benefits that
lazy-loading comes with, which is reducing bandwidth and wasting fewer networknetwork(
versus site, blog) resources, which in itself can impact Core Web Vitals (CWV) metrics.
The preferable default behavior here would be to find a solid middle ground between
those trade-offs. This leads to the following goal for the WordPress core implementation:

**The first “x” content image(s) should not be lazy-loaded by default, with “x” 
being as high as possible so that there is little to no LCP regressionregression
A software bug that breaks or degrades something that previously worked. Regressions
are often treated as critical bugs or [blockers](https://make.wordpress.org/core/2021/07/15/refining-wordpress-cores-lazy-loading-implementation/?output_format=md#blocker).
Recent regressions may be given higher priorities. A "3.6 regression" would be a
bug in 3.6 that worked as intended in 3.5. and as low as possible so that there 
is little to no regression in the total bytes loaded.**

(Note that this also includes iframes, but they are less commonly used as hero elements,
so for simplicity this post mentions primarily images.)

To determine the right value for “x” (i.e. the number of images/iframes to omit 
from being lazy-loaded by default), I conducted an analysis using two versions of
a small prototype pluginPlugin A plugin is a piece of software containing a group
of functions that can be added to a WordPress website. They can extend functionality
or add new features to your WordPress websites. WordPress plugins are written in
the PHP programming language and integrate seamlessly with WordPress. These can 
be free in the WordPress.org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
or can be cost-based plugin from a third-party. which prevents the `loading=”lazy”`
attribute from being added to the first 1 or 2 content images respectively. The 
analysis relied on the following methodology:

 * Test across the **50 most popular themes** according to the wordpress.orgWordPress.
   org The community site where WordPress code is created and shared by the users.
   This is where you can download the source code for WordPress core, plugins and
   themes as well as the central location for community conversations and organization.
   [https://wordpress.org/](https://wordpress.org/) popular themes search
 * Test on a demo site without any plugins active (except for the lazy-loading prototype
   plugin), for an archive view with 5 posts (where every post has a featured imageFeatured
   image A featured image is the main image used on your blog archive page and is
   pulled when the post or page is shared on social media. The image can be used
   to display in widget areas on your site or in a summary list of posts.) and for
   a single post view (where the post has a featured image as well as 5 images within
   its content), i.e. **2 different types of URLs**
 * Test for **2 viewports**, “mobile” and “desktop”
 * Use [WebPageTest](https://www.webpagetest.org) for the individual scenarios (
   200 in total), with 9 test runs for each scenario in order to counter variance
   and rely on the median result

That analysis produced the following results:

 * Omitting the** first content image** from being lazy-loaded resulted in a median
   LCP improvement of **7%** (1,877ms compared to 2,020ms with current core behavior)
   and a median image bytes increase of **0%** (368KB compared to 369KB with current
   core behavior). → Omitting the first content image clearly results in an LCP 
   improvement while not noticeably regressing on image bytes saved.
 * Omitting the **first two content images** from being lazy-loaded resulted in 
   a median LCP improvement of **5%** (1,927ms compared to 2,020ms with current 
   core behavior) and a median image bytes increase of **2%** (378KB compared to
   369KB with current core behavior). → Omitting the first two content images produces
   worse results for both metrics than only omitting the first one, i.e. it is better
   to only skip lazy-loading for the first content image, and therefore no additional
   tests with larger numbers of images not being lazy-loading are needed.
 * Both fixes actually perform even better in LCP compared to the results with lazy-
   loading completely disabled. This confirms that completely disabling lazy-loading
   is not a solution to the problem.
 * Drilling further into the results for omitting the **first content image**, 42%
   of scenarios result in a median LCP improvement of greater than 10%, with the
   maximum improvement being 33%. 5% of scenarios result in the median LCP being
   more than 10% worse, with the maximum here being 21%. → While the median LCP 
   improvement across all themes is only 7%, there are larger notable wins for a
   significant number of themes, while notable losses are minimal.

[[

Relative LCP change of the fix compared to the current behavior across all tested
scenarios

Following up on the findings on the LCP regression from lazy-loading as well as 
the analysis about the potential fix, the following refinement to the lazy-loading
implementation in core is being proposed for WordPress 5.9:

 * Instead of lazy-loading all images and iframes by default, the very first content
   image (also considering featured images) or content iframeiframe iFrame is an
   acronym for an inline frame. An iFrame is used inside a webpage to load another
   HTML document and render it. This HTML document may also contain JavaScript and/
   or CSS which is loaded at the time when iframe tag is parsed by the user’s browser.
   should not be lazy-loaded.
 * This is a more sensitive default than what the current implementation uses, that
   on average and at scale will result in better LCP performance out of the box,
   while keeping necessary bandwidth low.
 * Despite this change of the default behavior, responsibility for fine grained 
   control of which elements should be lazy-loaded remains with theme authors. As
   a theme author, you are still recommended to specify a `loading` attribute value
   for images presented by the theme. Particularly pay attention if your theme relies
   on less standard layouts, e.g. a grid or slider view of posts or if post content
   only appears far down the page.

A TracTrac An open source project by Edgewall Software that serves as a bug tracker
and project management tool for WordPress. ticketticket Created for both bug reports
and feature development on the bug tracker. for adding this enhancementenhancement
Enhancements are simple improvements to WordPress, such as the addition of a hook,
a new feature, or an improvement to an existing feature. to core has been opened
in [#53675](https://core.trac.wordpress.org/ticket/53675) for further review and
discussion.

_Props to [@addyosmani](https://profiles.wordpress.org/addyosmani/), [@adamsilverstein](https://profiles.wordpress.org/adamsilverstein/),
[@desrosj](https://profiles.wordpress.org/desrosj/) for review and proofreading._

[#feature-lazyloading](https://make.wordpress.org/core/tag/feature-lazyloading/)

### Share this:

 * [  Threads ](https://make.wordpress.org/core/2021/07/15/refining-wordpress-cores-lazy-loading-implementation/?share=threads)
 * [  Mastodon ](https://make.wordpress.org/core/2021/07/15/refining-wordpress-cores-lazy-loading-implementation/?share=mastodon)
 * [  Bluesky ](https://make.wordpress.org/core/2021/07/15/refining-wordpress-cores-lazy-loading-implementation/?share=bluesky)
 * [  X ](https://make.wordpress.org/core/2021/07/15/refining-wordpress-cores-lazy-loading-implementation/?share=x)
 * [  Facebook ](https://make.wordpress.org/core/2021/07/15/refining-wordpress-cores-lazy-loading-implementation/?share=facebook)
 * [  LinkedIn ](https://make.wordpress.org/core/2021/07/15/refining-wordpress-cores-lazy-loading-implementation/?share=linkedin)

 1.  ![](https://secure.gravatar.com/avatar/6ddc525ae47cdce6c4d632d40ad0ebdfa590fdb36dfca0c85a4cbd5cccaf5351?
     s=32&d=mm&r=g)
 2.   [Fernando Tellado](https://profiles.wordpress.org/fernandot/)  6:22 am _on_ July
     16, 2021
 3.  Very good test and proposal [@flixos90](https://profiles.wordpress.org/flixos90/)
 4.  I think that the site logo should be added to the lazy loading exclusion ’cause
     it always will be loaded above the fold in almost every theme.
 5.  Other candidates could be any image added to headerHeader The header of your site
     is typically the first thing people will experience. The masthead or header art
     located across the top of your page is part of the look and feel of your website.
     It can influence a visitor’s opinion about your content and you/ your organization’s
     brand. It may also look different on different screen sizes. menus o anything 
     else loaded in the header area, like header images, header backgrounds and so 
     on.
 6.   * ![](https://secure.gravatar.com/avatar/a60abe9a2c42fe5cf2117b88aac2eab186f0ea3c969f1f4a29d4a09d831153b8?
        s=32&d=mm&r=g)
      *  [Felix Arntz](https://profiles.wordpress.org/flixos90/)  6:46 pm _on_ July
        16, 2021
      * Good point, I agree we should make sure `get_custom_logo()` isn’t lazy-loaded
        either, at least the first time it is used on a page (which would cater for
        sites that also include the logo again in their footer).
      * The headerHeader The header of your site is typically the first thing people
        will experience. The masthead or header art located across the top of your 
        page is part of the look and feel of your website. It can influence a visitor’s
        opinion about your content and you/ your organization’s brand. It may also 
        look different on different screen sizes. image is already not lazy-loaded 
        with the current coreCore Core is the set of software required to run WordPress.
        The Core Development Team builds WordPress. implementation, so that should 
        be good to go.
      * I agree with your other statements as well, but for some of the other scenarios
        there is no standardized approach in WordPress for how to include those images,
        so it wouldn’t really be possible for core to make decisions on that.
 7.  ![](https://secure.gravatar.com/avatar/803828b32ae6d62391e6c26ff32e1a6e82064f8e4a29472876bac75731bca23c?
     s=32&d=mm&r=g)
 8.   [Jonny Harris](https://profiles.wordpress.org/spacedmonkey/)  12:41 pm _on_ July
     16, 2021
 9.  I wonder if we add some sort of global counter. So everytime that code adds the
     lazy attribute to an image, to increment the counter. Then we could, for example,
     say that the first three images, do not add the attribute and add to the rest.
     This doesn’t always work, as sometimes images are loaded in memory and rendered
     later. But it might something to consider.
 10. With custom themes, it is almost impossible to know when images are render. Images
     should be output in the footer, but then css could be used to put them in the 
     headerHeader The header of your site is typically the first thing people will 
     experience. The masthead or header art located across the top of your page is 
     part of the look and feel of your website. It can influence a visitor’s opinion
     about your content and you/ your organization’s brand. It may also look different
     on different screen sizes., for example.
 11. I wonder too, if we should have changes to the default themes and _s theme, to
     make sure to add loading=’eager’ where needed, to be a good example for other 
     theme developers.
 12.  * ![](https://secure.gravatar.com/avatar/a60abe9a2c42fe5cf2117b88aac2eab186f0ea3c969f1f4a29d4a09d831153b8?
        s=32&d=mm&r=g)
      *  [Felix Arntz](https://profiles.wordpress.org/flixos90/)  6:48 pm _on_ July
        16, 2021
      * That’s precisely what I had in mind and what I used in the prototype pluginPlugin
        A plugin is a piece of software containing a group of functions that can be
        added to a WordPress website. They can extend functionality or add new features
        to your WordPress websites. WordPress plugins are written in the PHP programming
        language and integrate seamlessly with WordPress. These can be free in the 
        WordPress.org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
        or can be cost-based plugin from a third-party. which I ran the analysis with.
      * I agree it cannot be 100% reliable due to factors like the ones you’re staging,
        but the analysis has shown that counting content images and omitting the first
        one from being lazy-loaded brings the LCP improvements we’re hoping for at 
        scale.
      * +1 to setting good examples in default themes.
         - ![](https://secure.gravatar.com/avatar/803828b32ae6d62391e6c26ff32e1a6e82064f8e4a29472876bac75731bca23c?
           s=32&d=mm&r=g)
         -  [Jonny Harris](https://profiles.wordpress.org/spacedmonkey/)  8:47 am _on_
           July 20, 2021
         - I helped with the original implementation of lazy loading and I am willing
           to help again here. [@flixos90](https://profiles.wordpress.org/flixos90/)
           do feel free to pingPing The act of sending a very small amount of data 
           to an end point. Ping is used in computer science to illicit a response 
           from a target server to test it’s connection. Ping is also a term used by
           Slack users to @ someone or send them a direct message (DM). Users might
           say something along the lines of “Ping me when the meeting starts.” on related
           tickets for code review. I would also love to see your prototype pluginPlugin
           A plugin is a piece of software containing a group of functions that can
           be added to a WordPress website. They can extend functionality or add new
           features to your WordPress websites. WordPress plugins are written in the
           PHP programming language and integrate seamlessly with WordPress. These 
           can be free in the WordPress.org Plugin Directory [https://wordpress.org/plugins/](https://wordpress.org/plugins/)
           or can be cost-based plugin from a third-party., if you are willing to share
           it.
         - Is there a theme maintainers team we can also ping to get them onboard?
 13. ![](https://secure.gravatar.com/avatar/81cca480f962f22c0d9563014548d25614824f4f7cd41975a3ca337b0c7a9c26?
     s=32&d=mm&r=g)
 14.  [Luke](https://profiles.wordpress.org/wpfed/)  1:24 pm _on_ July 16, 2021
 15. I think I’m more impressed with the testing process! That is a smart, keep it 
     simple approach to handling this situation.
 16. ![](https://secure.gravatar.com/avatar/81cca480f962f22c0d9563014548d25614824f4f7cd41975a3ca337b0c7a9c26?
     s=32&d=mm&r=g)
 17.  [Luke](https://profiles.wordpress.org/wpfed/)  1:58 pm _on_ July 16, 2021
 18. I just have a question about wp_set_image_loading_attr hook, is this not in the
     coreCore Core is the set of software required to run WordPress. The Core Development
     Team builds WordPress.? I can’t find any documentation on it or even plugins using
     it?
 19.  * ![](https://secure.gravatar.com/avatar/a60abe9a2c42fe5cf2117b88aac2eab186f0ea3c969f1f4a29d4a09d831153b8?
        s=32&d=mm&r=g)
      *  [Felix Arntz](https://profiles.wordpress.org/flixos90/)  6:49 pm _on_ July
        16, 2021
      * I think you may be looking for [this one](https://developer.wordpress.org/reference/hooks/wp_img_tag_add_loading_attr/)?
         - ![](https://secure.gravatar.com/avatar/81cca480f962f22c0d9563014548d25614824f4f7cd41975a3ca337b0c7a9c26?
           s=32&d=mm&r=g)
         -  [Luke](https://profiles.wordpress.org/wpfed/)  3:42 pm _on_ July 19, 2021
         - Yes, thanks [@flixos90](https://profiles.wordpress.org/flixos90/), had the
           wrong hook name!
 20. ![](https://secure.gravatar.com/avatar/9149875a8cf2875474223005576f7c412e7b4d07df82160adc687a8252227a5d?
     s=32&d=mm&r=g)
 21.  [Hareesh](https://profiles.wordpress.org/hareesh-pillai/)  3:46 am _on_ July 
     17, 2021
 22. Solid testing and presentation of the results. Kudos [@flixos90](https://profiles.wordpress.org/flixos90/)
     
     Making this change will go a long way on improving the performance in millions
     of sites of worldwide.

Comments are closed.

# Post navigation

[← WordPress 5.8 Release Candidate 4](https://make.wordpress.org/core/2021/07/15/wordpress-5-8-release-candidate-4/)

[WordPress 5.8 Release Day Process →](https://make.wordpress.org/core/2021/07/19/wordpress-5-8-release-day-process/)