-
Notifications
You must be signed in to change notification settings - Fork 135
Generate picture source elements when wp_calculate_image_srcset disable. #2201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Conversation
…is disabled - Add fallback to create simple source elements without srcset when wp_calculate_image_srcset returns false - - Enhance test coverage to verify source elements are created when srcset generation is disabled
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #2201 +/- ##
==========================================
- Coverage 68.68% 68.62% -0.06%
==========================================
Files 90 90
Lines 8093 8100 +7
==========================================
Hits 5559 5559
- Misses 2534 2541 +7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
is_string( $sizes ) ? sprintf( ' sizes="%s"', esc_attr( $sizes ) ) : '' | ||
); | ||
} else { | ||
// Fallback when wp_calculate_image_srcset returns false - generate simple source without srcset. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SOURCE
still has a srcset
(as it can't have src
).
// Fallback when wp_calculate_image_srcset returns false - generate simple source without srcset. | |
// Fallback when wp_calculate_image_srcset returns false - generate simple non-responsive srcset. |
$this->assertStringContainsString( '.webp', $picture_processor->get_attribute( 'srcset' ), 'Make sure the Picture source srcset should return WEBP images.' ); | ||
} | ||
|
||
// Ensure at least one source element was created (addresses GitHub issue with wp_calculate_image_srcset disabled). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice to add a separate test that sets the wp_calculate_image_srcset filter to return false and verifies the source generation.
@sarthak-19 - thanks for the PR! Can you check this comment - #2191 (comment) Can you share the HTML you are seeing output Before and After your patch, this will help in understanding the nature of the problem more clearly. |
Fix: #2191
Problem
When users disable responsive image srcset generation via
add_filter('wp_calculate_image_srcset', '__return_false')
, the picture element functionality was broken - no<source>
elements were being generated for modern image formats like AVIF and WebP.This resulted in the picture element only containing the fallback
<img>
tag, defeating the purpose of serving modern image formats.Root Cause
The picture element generation logic only had two paths:
<source>
elements with full srcset attributes<source>
elements usingwebp_uploads_get_mime_type_image()
However, there was a gap when the IMG tag had
srcset
andsizes
attributes, butwp_calculate_image_srcset()
returnedfalse
. In this case, the code attempted the first path but failed silently when srcset generation was disabled.