1

I'm noticing a difference (bug?) in how WP is rendering srcset images now.

If I register my image sizes with

add_image_size('single_featured_image', 1280, 9999, false);
add_image_size('single_featured_image_2X', 2560, 9999, false);

and confirm the files generated

test-image-1280x720.jpg test-image-2560x1440.jpg

and then attempt to render them with

          echo wp_get_attachment_image(
                get_post_thumbnail_id(),
                'single_featured_image',
                false,
                [
                  'class' => 'object-cover object-center w-full h-full'
                ]
              );

HTML rendered is

<img width="1280" height="720" src="http://wauble.lndo.site/wp-content/uploads/2023/07/test-image-2-1280x720.jpg" class="object-cover object-center w-full h-full" alt="" decoding="async" loading="lazy" srcset="http://wauble.lndo.site/wp-content/uploads/2023/07/test-image-2-1280x720.jpg 1280w, http://wauble.lndo.site/wp-content/uploads/2023/07/test-image-2-300x169.jpg 300w, http://wauble.lndo.site/wp-content/uploads/2023/07/test-image-2-1024x576.jpg 1024w, http://wauble.lndo.site/wp-content/uploads/2023/07/test-image-2-768x432.jpg 768w, http://wauble.lndo.site/wp-content/uploads/2023/07/test-image-2-1536x864.jpg 1536w, http://wauble.lndo.site/wp-content/uploads/2023/07/test-image-2-2048x1152.jpg 2048w, http://wauble.lndo.site/wp-content/uploads/2023/07/test-image-2-18x10.jpg 18w" sizes="(max-width: 1280px) 100vw, 1280px">

Notice 2560x is not included in the srcset values.

I'm assuming there is some proportion matching going on. What's strange is I used to use this technique all the time and always seemed to work.

I can get the 2x image included in srcset values if instead of using the image name handle I use the int[] for image size argument in my wp_get_attachment_image call.

       echo wp_get_attachment_image(
                get_post_thumbnail_id(),
                [1280, 9999],
                false,
                [
                  'class' => 'object-cover object-center w-full h-full'
                ]
              );

No real problem I guess, just seems a little silly that I have to name it in the first place. I apologize if I'm doing something dumb here....and I hope that's the case.

Any help appreciated!

1 Answer 1

2

It appears the problem was that image size variants were being capped by the max_srcset_image_width property that can be raised with the following:

add_filter( 'max_srcset_image_width', 'setSrcsetMaximumWidth', 10, 2 );

function setSrcsetMaximumWidth( $max_srcset_image_width, $sizes_array ) {
  return 3200;
}

I was never aware of this before and have no idea why I never ran into this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.