Site logo

Archived topic

How to make WP Rocket not to lazy load featured image

8 replies · Started by Sunil on May 22, 2021

Viewing posts 1–9 of 9

Hello

What class should I use in WP rocket to stop it from lazy loading my featured images as lazy loading featured images increases my LCP.

Hi there,

add this PHP Snippet to your site.

// Add first-featured-image ( or any class ) to featured image of latest post
function skip_lazy_class_first_featured_image($attr) {
  global $wp_query;
  if ( 0 == $wp_query->current_post ) {
      $attr['class'] .= ' first-featured-image';  
  }
  return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'skip_lazy_class_first_featured_image' );

Then you can use the first-featured-image class in the WP Rocket options to exclude it from your lazy loader.

Done.

Hello David

Sorry for reopening this thread. Can you guide me how I can stop lazy load for the featured image on the AMP pages as well?

They're still loaded on amp pages. I use official AMP plugin.

You would need to check with the AMP plugin developer, as it is 'swapping' out the images for its own code, and i don't know what ( if any ) filters exist for changing their images HTML

Hello everyone,

sorry to reopen this thread. Is there a way to modify the snippet in order not to lazy load the first TWO posts?

Thanks,

Mario

Hi there,

try:

// Add first-featured-image ( or any class ) to featured image of latest post
function skip_lazy_class_first_featured_image($attr) {
  global $wp_query;
  if ( 0 == $wp_query->current_post || 1 == $wp_query->current_post ) {
      $attr['class'] .= ' first-featured-image';  
  }
  return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'skip_lazy_class_first_featured_image' );

Hi David,
thanks. And what about the class in WP-Rocket settings? Can I keep first-featured-image or do I have to make a change?

No change to WP rocket, the same class will be added to both images.

This archived topic is closed to new replies.