-
Notifications
You must be signed in to change notification settings - Fork 101
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
Fix image sizes and srcset for intermediate images #2007
Conversation
Use a new filter available in WP 5.7 to properly pass image dimensions for the image srcset.
Will those who are still on WP versions before 5.7, continue to see the same full size image for each named image size (i.e. the Before)? |
* @param string $context Additional context about how the function was called or where the img tag is. | ||
* @param int $attachment_id The image attachment ID. | ||
*/ | ||
function a8c_files_maybe_inject_dimensions( $value, $image, $context, $attachment_id ) { |
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.
Any reason not to use type declarations on these parameters and return?
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.
Good question! We're not really using most of these since we're just returning true
for the value in this filter and we're not returning any of them either, so I wasn't sure if that would be helpful in this case. Would you suggest still adding them? I also wasn't sure if I should pass them all even if we're not using them?
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.
If you're not using them, then there is no need to support them in the function signature, unless a param to the right is being used.
However, since you need $attachment_id
, then they all need to be passed.
The non-first types should be known, as they are from core code, but the first type may have been filtered at an earlier priority into a different type, so although it's not being used here, the advantage of using types here is that it becomes an extra check in case some other code is doing something wrong. It's also good for IDEs and static analysis tools, like Psalm or Phpstan
Adding the return type also has no drawbacks.
Thanks for your review @GaryJones! I've addressed the issues and separated out the dimension logic into another function.
I tested this and for versions before 5.7, site's will continue to see the same |
We're investigating other avenues, in the meantime, we'll keep it open but with the appropriate status. |
Is there a work around for this atm? There is an open ticket here #128167-z that I would like to reply to |
Just leaving a note that this issue came up in #145657-z. |
529fa93
to
1ac9637
Compare
This pull request has been marked stale because it has been open for 60 days with no activity. If there is no activity within 7 days, it will be closed. This is an automation to keep pull requests manageable and actionable and is not a comment on the quality of this pull request nor on the work done so far. Closed PRs are still valuable to the project and their branches are preserved. |
Description
This PR uses a new filter available in WP 5.7 to properly pass image dimensions for the image
srcset
andsizes
.This needs to be tested and merged in coordination with the WP 5.7 release (March 9 is the current tentative release date).
New filter added in WP 5.7: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/media.php?rev=50136#L1643
How the fix works:
wp_img_tag_add_width_and_height_attr
which is early enough that our image source still has the query arguments available so we know what the image size is intended to bewp_image_src_get_dimensions
where we set image dimensionsChangelog Description
Bug Fix: Fixed an issue where images were not properly sized when using the Block Editor and intermediate image sizes.
Previously, all images added through the Block Editor that were assigned intermediate image sizes ( e.g. Thumbnail ) would appear as full size images.
Checklist
Please make sure the items below have been covered before requesting a review:
Steps to Test
wp-includes/media.php
file to include the changes towp_image_src_get_dimensions
found here: https://core.trac.wordpress.org/browser/trunk/src/wp-includes/media.php?rev=50136#L1601srcset
andsizes
attributes output are correct.Before the change:
After the change: