Opened 3 months ago
Last modified 3 months ago
#59331 new defect (bug)
Image optimizations fail to apply when using Timber
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | 6.3 |
Component: | Media | Keywords: | |
Focuses: | performance | Cc: |
Description (last modified by )
A user reported hero images were getting loading=lazy
on their site. After inquiring, I found they were using Timber for templating. It makes sense that the logic in wp_get_loading_optimization_attributes()
wouldn't work properly in this case since it is tightly bound to WordPress's templating system(s). This may actually be an issue better fixed in Timber itself, but ideally WordPress core should be able to do the right thing with alternate templating systems (or even complex templates written in the WordPress way). Ultimately, this may require the use of output buffering and HTML Tag Processor as proposed for a module in the Performance Lab plugin. See #59331 for adding output buffering for template renders with a filter for post-processing.
Here's how to reproduce the issue with Timber:
- Install and activate the Timber plugin from WordPress.org.
- Install and activate the Timber Starter Theme from GitHub (i.e. download the ZIP).
- Add a new post with an initial large image or a featured image.
- See the image has
loading=lazy
and is missingfetchpriority=high
on the frontend.
Initial image block:
<figure class="wp-block-image"> <img decoding="async" loading="lazy" width="1300" height="1300" src="https://example.org/wp-content/uploads/2023/09/image.jpeg" alt="" class="wp-image-8" srcset=" https://example.org/wp-content/uploads/2023/09/image.jpeg 1300w, https://example.org/wp-content/uploads/2023/09/image-300x300.jpeg 300w, https://example.org/wp-content/uploads/2023/09/image-1024x1024.jpeg 1024w, https://example.org/wp-content/uploads/2023/09/image-150x150.jpeg 150w, https://example.org/wp-content/uploads/2023/09/image-768x768.jpeg 768w " sizes="(max-width: 1300px) 100vw, 1300px" /> </figure>
Featured image markup:
<img src="https://timber-image-optimization.instawp.xyz/wp-content/uploads/2023/09/image-1200x300-c-default.jpeg">
Here's a workaround plugin that addresses the issue with
the_content
images in a Timber-based theme: https://gist.github.com/westonruter/456afa3cbf29563ffd590401988f1a23It uses HTML Tag Processor to optimize images in the first
the_content
:loading
attribute removed.fetchpriority=high
.