WordPress 5.5 lazy-loads images: keep the hero image eager
Browsers support native lazy loading with the loading="lazy" attribute since Chrome 76 (July 2019) and Firefox 75 (April 2020). WordPress 5.5, released on August 11, 2020, adds the attribute automatically to images in post content, in text widgets, to avatars and to images output with wp_get_attachment_image(), including featured images. Most sites got lazy loading for free with the update. The images at the top of the page, though, should not wait: a lazy hero image is loaded later than before, and the Largest Contentful Paint gets worse.
Problem: After the update to WordPress 5.5, the large featured image at the top of every post has loading="lazy", and the LCP time in PageSpeed Insights went up.
Solution: Pass 'loading' => 'eager' (or false) in the attributes of the hero image, and use the wp_lazy_loading_enabled filter to switch lazy loading off for a whole context if needed.
The hero image
The attributes array of the image functions overrides the automatic value:
<?php // single.php: the first image the visitor sees the_post_thumbnail( 'full', array( 'loading' => 'eager', 'class' => 'hero__image' ) ); // or echo wp_get_attachment_image( $hero_id, 'ha-hero', false, array( 'loading' => false ) ); // no attribute at all
false removes the attribute, 'eager' sets it explicitly; for the browser both mean “load now”.
Switching it off for a context
<?php
// No lazy loading at all
add_filter( 'wp_lazy_loading_enabled', '__return_false' );
// Only for images inside post content, e.g. on a site where posts start with a big image
add_filter( 'wp_lazy_loading_enabled', function ( $default, $tag_name, $context ) {
if ( 'img' === $tag_name && 'the_content' === $context ) {
return false;
}
return $default;
}, 10, 3 );
The contexts in 5.5 are the_content, the_excerpt, widget_text_content, get_avatar and wp_get_attachment_image. Lazy loading of iframes is not part of 5.5.
Checking the result
<?php // WordPress 5.5+: these get loading="lazy" the_post_thumbnail( 'large' ); echo wp_get_attachment_image( 42, 'medium' ); echo get_avatar( $user_id, 96 ); // this does not the_post_thumbnail( 'large', array( 'loading' => 'eager' ) );
Look at the page source, or in PageSpeed Insights at “Largest Contentful Paint element”: if it is an image with loading="lazy", that is the image to change.
Images need their width and height attributes for lazy loading to work without layout jumps; images from the media library have them, hand-written <img> tags in templates often do not.
Update (2023): WordPress 5.9 stopped adding loading="lazy" to the first content image, and WordPress 6.3 (2023) skips lazy loading for images it detects above the fold and adds fetchpriority="high" to the likely LCP image. Explicit 'loading' => 'eager' on the hero image is still the most reliable way.
NOTE: “Above the fold” depends on the screen. An image that is the third block on desktop may be off-screen on a phone and fine to lazy-load. Make only the one or two images that are visible on every screen eager, and leave the rest to WordPress.