You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
wp media replace changes the path of the attached file, which potentially breaks links #274
"wp media replace" uploads a new file, and adjusts the attachment post to reflect the new location of the new file in the upload folder.
The path to the uploaded file therefore changes, even if the attachment id remains the same, and existing links to the file break.
The Gutenberg editor often creates direct links to uploaded files.
It is not a seamless replacement of the attached file.
Describe how other contributors can replicate this bug
Replace an older media file using "wp media replace", so the new file ends in a different folder in the upload directory.
Watch links to the old path fail.
Describe what you would expect as the correct outcome
The attached file should be replaced in place, keeping the old path, and if necessary, uploading the file to the old upload folder, as not to create broken image links on the site.
Let us know what environment you are running this on
This is a valid concern. I've been thinking about it a good bit this morning, and here's what I've come up with so far...
One problem with "replace in place" is that if you're replacing wp-content/uploads/2023/10/31/halloween.jpg with halloween.png is that the extension mismatch leaves you in the exact same problem as the original bug.
So, one possible approach is to do the "replace in place", but only if the extension matches exactly.
If a different extension is detected, we would need to decide which of the following is preferable:
Just error out and refuse the replace. The error message should explain why (mismatched file type).
wp media regenerate (link) offers –update-attachment-refs as an optional flag, which will look through all non-revision posts and update references to regenerated media. A similar flag could be added to wp media replace, if it doesn't already exist.
We could add an optional flag to convert the new media to the old format, if possible. Obvious downsides are that not all media is convertible, some formats convert but lose features (alpha transparency, animation, etc.), and most conversions experience some loss of quality in the process.
Option 2 only works in posts (I believe), so references in custom fields, widget, etc., would all be missed.
Activity
nathanrice commentedon Nov 3, 2026
This is a valid concern. I've been thinking about it a good bit this morning, and here's what I've come up with so far...
One problem with "replace in place" is that if you're replacing
wp-content/uploads/2023/10/31/halloween.jpgwithhalloween.pngis that the extension mismatch leaves you in the exact same problem as the original bug.So, one possible approach is to do the "replace in place", but only if the extension matches exactly.
If a different extension is detected, we would need to decide which of the following is preferable:
wp media regenerate(link) offers–update-attachment-refsas an optional flag, which will look through all non-revision posts and update references to regenerated media. A similar flag could be added towp media replace, if it doesn't already exist.Option 2 only works in posts (I believe), so references in custom fields, widget, etc., would all be missed.