Skip to content

Link to image file should point to original upload regardless of fallback setting#2570

Open
faisalahammad wants to merge 2 commits into
WordPress:trunkfrom
faisalahammad:fix/2470-gallery-link-original-image
Open

Link to image file should point to original upload regardless of fallback setting#2570
faisalahammad wants to merge 2 commits into
WordPress:trunkfrom
faisalahammad:fix/2470-gallery-link-original-image

Conversation

@faisalahammad

Copy link
Copy Markdown

Summary

Fixes #2470

Relevant technical choices

When "Also generate fallback images in the original upload format" is disabled, Modern Image Formats swaps the attachment's main file for the generated modern-format version and backs up the true original in the original_image metadata. Because of that, wp_get_attachment_url() legitimately returns the modern-format file, and the "Link to image file" href on Image/Gallery blocks (built from the REST source_url field) ends up pointing to it instead of the original upload. With fallback enabled this doesn't happen, so the link behavior was inconsistent depending on that one setting - as discussed and agreed on in the issue thread.

This PR filters the REST attachment response so source_url (and media_details.sizes.full.source_url) point back to the original file when this plugin has swapped formats. Detection compares the mime type of the currently attached file against the backed-up original's mime type, so it only triggers on this plugin's own format swap and leaves core's own unrelated "-scaled" resizing (same mime type) untouched. The <img> tag rendering pipeline is untouched too - it still shows the modern format as before, only the link/href changes.

This affects new image insertions going forward, since Gutenberg bakes the href into the block's stored HTML at insert time. Already-published posts keep their existing link until the image is re-inserted or the block is re-saved.

Added plugins/webp-uploads/tests/test-attachment-url.php covering: format swap detected, fallback enabled (no-op), core's own scaled images (no-op), and img tag still showing the modern format.

Use of AI Tools

Used Claude Code as a coding assistant for this PR: investigating the issue thread and the plugin's upload/rewrite pipeline, drafting the fix and its tests. I reviewed the logic, ran the full webp-uploads PHPUnit suite, phpcs, and phpstan locally, and verified the fix and edge cases manually before opening this PR.

… setting

- REST attachment source_url now points to the original upload when this
  plugin swapped the main file for a modern format (fallback setting off)
- core's own -scaled resizing is left untouched, only our own format swap
  is detected and corrected
- img tag rendering still shows the modern format as before

Link to image file should point to the original upload no matter the
fallback setting. Right now it only does that when fallback is on,
since with fallback off the plugin swaps the attachment file itself
to the modern format and wp_get_attachment_url() follows that swap.

Fixes WordPress#2470
@github-actions github-actions Bot added the [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) label Jul 5, 2026
@github-actions

github-actions Bot commented Jul 5, 2026

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: faisalahammad <faisalahammad@git.wordpress.org>
Co-authored-by: adamsilverstein <adamsilverstein@git.wordpress.org>
Co-authored-by: b1ink0 <b1ink0@git.wordpress.org>
Co-authored-by: ms-studio <targz-1@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@codecov

codecov Bot commented Jul 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.95652% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.40%. Comparing base (8d70987) to head (a324265).
⚠️ Report is 15 commits behind head on trunk.

Files with missing lines Patch % Lines
plugins/webp-uploads/rest-api.php 86.95% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #2570      +/-   ##
==========================================
+ Coverage   70.35%   70.40%   +0.04%     
==========================================
  Files          91       91              
  Lines        7867     7890      +23     
==========================================
+ Hits         5535     5555      +20     
- Misses       2332     2335       +3     
Flag Coverage Δ
multisite 70.40% <86.95%> (+0.04%) ⬆️
single 35.06% <0.00%> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@adamsilverstein adamsilverstein added the [Type] Bug An existing feature is broken label Jul 12, 2026
Comment thread plugins/webp-uploads/rest-api.php
@adamsilverstein

Copy link
Copy Markdown
Member

@ms-studio can you check to see if this resolves your issue?

For oversized images that WordPress scales to '-scaled', the REST source_url
filter now points at the swapped scaled output-format file
(leaves-scaled.webp) instead of the unscaled original upload (leaves.jpg),
matching the reviewer's expectation for the Link to image file href.

Non-scaled swapped images still resolve to metadata.original_image as before.

Addresses PR feedback in WordPress#2570.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Bug An existing feature is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linked images in galleries aren't replaced

2 participants