WordPress / WordPress/performance

Consider supplying modern image format in response to `wp_get_attachment_image()`

Open
#2,299 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

[Plugin] Modern Image Formats [Type] Enhancement
Dominant language
PHP
Stars
461
Forks
165
Avg merge
11h 12m
Merged PRs (30d)
28

Description

We should consider filtering the response to wp_get_attachment_image() to handle additional cases where a theme may be printing an image in ways other than the_content() and the_post_thumbnail().

Originally posted by @b1ink0 in #2178:

I was able to replicate this bug. It seems the issue is with the webp_uploads_update_featured_image() function hooked to post_thumbnail_html, which only calls webp_uploads_img_tag_update_mime_type() and does not conditionally call webp_uploads_wrap_image_in_picture() based on webp_uploads_is_picture_element_enabled().

And, right now we are using the post_thumbnail_html filter for the featured image, but we could use the wp_get_attachment_image filter as the bug reporter suggested. However, this filter would also include any image echoed using the wp_get_attachment_image() function, which is used for featured images and is also typically used by other plugins. So, I'm not sure whether the Modern Image Formats plugin should also handle those images.

The following was fixed by https://github.com/WordPress/performance/pull/2179:

Also, while trying to replicate this bug, I noticed that (Using Twenty Twenty-One) Image Widgets or widget blocks containing the Image block and Gallery block don’t get their sources replaced with modern image formats because the webp_uploads_wrap_image_in_picture() function only allows the the_content context, while widgets send the widget_block_content context.

See also https://github.com/WordPress/performance/pull/2179#discussion_r2376479942 from @adamsilverstein:

lets avoid altering other plugin output for now, the unexpected change could break things or cause conflicts.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with webp-uploads/hooks.php, especially webp_uploads_update_featured_image() and webp_uploads_img_tag_update_mime_type(), then compare them with webp_uploads_wrap_image_in_picture() in picture-element.php and webp_uploads_is_picture_element_enabled() in helper.php. Clarify whether wp_get_attachment_image() output should be changed without altering other plugin output, and define the expected behavior before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.