How to disable lazy loading in WordPress 5.5+

Looking to disable lazy loading in WordPress 5.5 or above? All you may need is to add the following code snippet into your functions.php file.

Remove default lazy loading in WordPress

In order to disable lazy loading in WordPress by default, you should add the following code into your functions.php file.

add_filter( 'wp_lazy_loading_enabled', '__return_false' );

Disable lazy loading for specific image attachments

If you’re only looking to disable lazy loading for a specific media attachment, you should use this code below.

This code can be added into your functions.php file and the media attachment ID can be changed to your desired attachment’s ID.

function remove_lazy_loading_for_specific_id( $value, $image, $context ) {
     if ( 'the_content' === $context ) {
          $image_url = wp_get_attachment_image_url( 100, 'large' ); // Change attachment ID and size accordingly
          if ( false !== strpos( $image, ' src="' . $image_url . '"' ) {
               return false;
          }
     }
     return $value;
}
add_filter( 'wp_img_tag_add_loading_attr', 'remove_lazy_loading_for_specific_id', 10, 3 );

Another method of achieving the same result is by using wp_get_attachment_image directly as follows:

<?php echo wp_get_attachment_image( 100, 'large', false, array( 'loading' => false ) ); // Change attachment ID and size accordingly ?>