-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Print IMG
auto-sizes contain CSS fix by enqueueing inline style
#8954
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
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
wp_add_inline_style( $handle, 'img:is([sizes="auto" i], [sizes^="auto," i]) { contain-intrinsic-size: 3000px 1500px }' ); | ||
|
||
// Make sure inline style is printed first. | ||
array_unshift( wp_styles()->queue, $handle ); |
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.
This seems a bit hacky to me, but I understand the purpose. I assume there's not a better way to control the order of registered styles besides maybe making sure that the wp_register_style()
call is made on an earlier hook, like init
, correct?
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.
Exactly.
src/wp-includes/media.php
Outdated
* @since 6.9.0 The style is now enqueued as opposed to being printed. | ||
* | ||
* @link https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size | ||
* @link https://core.trac.wordpress.org/ticket/62413 | ||
*/ | ||
function wp_print_auto_sizes_contain_css_fix() { |
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.
I don't love this name now, since it's implying this function does something that it doesn't.
We could potentially deprecate (effectively rename) it, though let's discuss how reasonable that would be.
- Obviously there's a BC risk if people expect this function to be hooked into
wp_head
and want to remove it to disable the snippet. - However, there's also a BC risk if we just change what this function does, in case someone calls it manually somewhere else.
- If someone unhooks this function to disable the snippet, and we wanted to replace it with a new function, we could still add the hook first, see if it is removed, and only before the function would be called ensure it is not invoked. This way we know whether it was removed, and could "forward" this removal to the new function.
- I believe we did something like that in Core somewhere else before, but I can't recall where exactly.
- A nice side-effect of deprecating / replacing would be that we could also use the proper hook
wp_enqueue_scripts
for the new function.
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.
@felixarntz see e7144a3 for that which was the original approach I took.
I believe we did something like that in Core somewhere else before, but I can't recall where exactly.
Yes. We did it with printing the emoji styles in Core-58775.
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.
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 we go with the function rename and move from wp_head
to wp_enqueue_scripts
, since this function was introduced with the wp_img_tag_add_auto_sizes
filter to disable the style, it seems overkill to also retain back-compat to see if someone removed the action on wp_head
. If they had done this, they were essentially doing it wrong. There are only 4 examples of this being done on GitHub.
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.
I've opened pull requests to use the filter instead:
- Use correct mechanism to disable auto-sizes CSS fix szepeviktor/zero-bytes-theme#3
- Use correct mechanism to disable auto-sizes CSS fix yuupiyo/MyPortfolio#1
- Use correct mechanism to disable auto-sizes CSS fix pavelleonenko007/Utopia-wordpress-theme#1
- Use correct mechanism to disable auto-sizes CSS fix conedevelopment/base-starter-theme#4
In any case, if someone doesn't update the hook, the risk of breakage is very low, as it will just mean an additional STYLE
is output unexpectedly.
* | ||
* This rule overrides the similar rule in the default user agent stylesheet, to avoid images that use e.g. | ||
* `width: auto` or `width: fit-content` to appear smaller. | ||
* | ||
* @since 6.7.1 | ||
* @since 6.9.0 |
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.
This could be 6.8.2 if we want.
* @since 6.9.0 | |
* @since 6.8.2 |
* | ||
* @since 6.7.1 | ||
* @deprecated 6.9.0 Use wp_enqueue_img_auto_sizes_contain_css_fix() instead. |
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.
Per below, if we want to target the next minor release:
* @deprecated 6.9.0 Use wp_enqueue_img_auto_sizes_contain_css_fix() instead. | |
* @deprecated 6.8.2 Use wp_enqueue_img_auto_sizes_contain_css_fix() instead. |
Trac ticket: https://core.trac.wordpress.org/ticket/62731
Instead of manually printing the
STYLE
tag to fixsizes=auto
issue with images, this enqueues the inline style viaWP_Styles
. This ensures the styles are printed with other styles as opposed to being printed very early, even before theTITLE
tag. This also allows plugins to do additional processing of the stylesheet prior to it being printed. Note thatarray_unshift( wp_scripts()->queue, $handle )
is used instead ofwp_enqueue_style( $handle )
in order to ensure that the stylesheet is printed before any others, preserving the existing cascade.This is very similar to the change that was made to replace
print_emoji_styles()
withwp_enqueue_emoji_styles()
in Core-58775.Diff on rendered page (after Prettier formatting):
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.