[wp-trac] [WordPress Trac] #58089: Featured images within post content cause lazy-loading logic to apply incorrectly
WordPress Trac
noreply at wordpress.org
Tue Apr 4 19:02:59 UTC 2023
#58089: Featured images within post content cause lazy-loading logic to apply
incorrectly
------------------------------------------+------------------------------
Reporter: flixos90 | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 5.9
Severity: normal | Resolution:
Keywords: needs-testing-info has-patch | Focuses: performance
------------------------------------------+------------------------------
Old description:
> When including featured images within post content, the logic to omit the
> `loading="lazy"` attribute on the first x images may not be working
> correctly, depending on where the featured images are placed in the
> content.
>
> The reason for that is that each featured image calls
> `wp_get_loading_attr_default()` with a context of `the_post_thumbnail`
> ''while'' the post content is being parsed. In other words, `the_content`
> filter triggers the logic of adding lazy-loading attributes to the output
> using the the `the_content` context, but before that happens all blocks
> are parsed, including those that display featured images via
> `get_the_post_thumbnail()`. These calls then trigger the logic for
> featured images to apply ''within'' post content, but that logic is only
> intended to apply when featured images are displayed by themselves, i.e.
> outside of post content.
>
> I ran into this bug on my own website and was able to figure out a
> solution, but I haven't yet been able to replicate it on another site.
> That said, I am quite certain that it is a bug, but will need to
> investigate more closely to figure out how to replicate it with core.
> Potentially the only way to trigger it is using certain custom blocks.
>
> This bug was likely introduced by the original feature #56375, and is
> furthermore related to #56930, although the latter is likely not the
> cause for it.
New description:
When including featured images within post content, the logic to omit the
`loading="lazy"` attribute on the first x images may not be working
correctly, depending on where the featured images are placed in the
content.
The reason for that is that each featured image calls
`wp_get_loading_attr_default()` with a context of `the_post_thumbnail`
''while'' the post content is being parsed. In other words, `the_content`
filter triggers the logic of adding lazy-loading attributes to the output
using the the `the_content` context, but before that happens all blocks
are parsed, including those that display featured images via
`get_the_post_thumbnail()`. These calls then trigger the logic for
featured images to apply ''within'' post content, but that logic is only
intended to apply when featured images are displayed by themselves, i.e.
outside of post content.
I ran into this bug on my own website and was able to figure out a
solution, but I haven't yet been able to replicate it on another site.
That said, I am quite certain that it is a bug, but will need to
investigate more closely to figure out how to replicate it with core.
Potentially the only way to trigger it is using certain custom blocks.
This bug was likely introduced by the original feature #53675, and is
furthermore related to #56930, although the latter is likely not the cause
for it.
--
Comment (by flixos90):
I have opened a PR https://github.com/WordPress/wordpress-
develop/pull/4300 with the fix that resolves the bug on my own website,
for early consideration. Will still have to figure out whether this is a
bug that can happen from core itself or only via custom blocks that
include featured images.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/58089#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list