WordPress media library management

Swap a file's content without breaking a single link

Media Replace overwrites the file behind an attachment in place — same attachment ID, same URL, regenerated thumbnails — so every post, gallery and link that already references it just keeps working.

  • A Replace File button on every attachment, in the library and on its edit screen.
  • The URL and attachment ID never change.
  • Every image size regenerates automatically.
  • A warning appears if the replacement is a different file type.
Attachment ActionMedia Replace
Replace File×
Drag & drop a file hereor click to select
CancelReplace
Same URL, brand new fileEvery link and embed keeps working.
Active
Quick answer

What does Media Replace do?

Once the media_replace module is active, TOWP_Media_Replace adds a "Replace File" button to the media library and to each attachment's edit screen. Consequently, uploading a replacement overwrites the original file in place, deletes the old thumbnails, regenerates every registered image size against the new file, and updates the attachment's metadata, all under the exact same attachment ID and URL. Because browsers and caches would otherwise keep serving the old file from that same address, the module also stamps a version parameter across every place WordPress can render that URL, including directly into post_content in the database, so the new file actually shows up everywhere.

The "delete, re-upload, fix every link" problem

Swapping a file shouldn't mean starting over

Since a fresh upload always creates a new identity, correcting one file usually means chasing down every place the old one was used.

Deleting an attachment and uploading its replacement

As a result, WordPress assigns a brand new attachment ID and address, leaving every post, gallery and page that referenced the original pointing at nothing.

Even an in-place swap fights the browser's own cache

However, overwriting a file at the same URL only solves the broken-link problem; browsers and CDNs that already cached the old file at that exact address won't necessarily notice anything changed.

A dedicated WordPress solution

Same attachment, a different file behind it

Instead, this module replaces the file itself while keeping the attachment record, its ID and its URL completely untouched.

Same ID, same URL, a completely new file

Because nothing about its identity or address changes, nothing that already references the attachment needs to change either.

Every thumbnail regenerated automatically

Similarly, all registered image sizes get rebuilt against the new file, the same way they would for a fresh upload.

A warning before a type mismatch surprises anyone

If the replacement file is a different type than the original, a warning also appears before the swap goes through.

Verified feature set

Seven layers of cache-busting, one clean replacement

Accordingly, every capability below is present in the supplied PHP class, its JS asset and the settings registration.

Full metadata regeneration

Every image size rebuilt

Next, wp_generate_attachment_metadata() runs against the new file and regenerates every registered thumbnail size exactly as it would on first upload.

wp_generate_attachment_metadataFull regeneration
Aspect ratio changes handled

Old thumbnail references get updated too

Then, if a different aspect ratio changes a thumbnail's filename, the module updates every place that filename was referenced to the new one.

Filename change detectionSite-wide reference update
Cache-busting, everywhere

Seven separate rendering paths covered

Consequently, a version parameter gets appended to the attachment URL, content, JS/Gutenberg data, image_src calls, the REST API response and every srcset entry.

7 rendering pathsVersion parameter
Stamped directly into the database

Not just a runtime filter

Furthermore, that same version parameter gets written straight into post_content for every post referencing the file, since page caches and stored Gutenberg markup can otherwise bypass a PHP-only filter.

Direct DB updateBypasses page caching
Metadata reset for the new content

Alt text and caption cleared

Finally, since alt text, description and caption belonged to the original file's content, the module clears them rather than carrying them over to a file they no longer describe.

Alt text clearedFresh metadata
Practical use cases

Where an in-place swap helps most

Overall, this module supports any situation where a file needs correcting without touching what already references it.

Fixing a low-quality or incorrect upload

Because the URL never changes, swapping in a corrected version of a file doesn't require touching any of the posts, pages or galleries that already reference it.

Updating a document or a logo everywhere it's used

Similarly, replacing a PDF, a logo or a diagram in one place makes every embed across the site show the new version automatically.

Correcting a file uploaded with the wrong content entirely

Even so, a genuinely wrong file, of a different type even, can get fixed while keeping the original attachment's ID and any links already pointing at it.

Operational benefits

A file corrected, nothing else disturbed

Together, these combine a genuinely non-destructive fix with the cache-busting that makes it actually visible.

Nothing breaks anywhere on the siteEvery post, gallery, widget and direct link keeps referencing the same attachment, so nothing needs manual fixing afterward.
One upload instead of a multi-step fixInstead of deleting the old file, uploading a new one, and updating every reference by hand, a single replacement covers all of it.
The new file actually shows upBecause of the multi-layer cache-busting, visitors see the replacement immediately, not the stale cached version at the same address.
Permission-checked at every stepOnly users who can edit the specific attachment can replace it, since the module verifies this before touching any file.
Performance behaviour

Regenerated once, at the moment it matters

In fact, its heaviest work happens exactly once, during the replacement itself.

Regeneration happens once, at replacement time

Specifically, thumbnails rebuild during the replace action itself, not on subsequent page loads.

A targeted database update, not a full scan

Likewise, only posts referencing the replaced file's path get searched by the content-stamping step, not every post on the site.

Assets load only where the button appears

Additionally, enqueueing happens only on the media library and post editor screens, where the Replace File button can actually appear.

Security implementation

Checked at every step, nothing skipped

Before any code touches anything on disk, permission, nonce and file type all get verified.

01

Capability checks before any file is touched

Specifically, both upload_files and edit_post on the specific attachment are required before a replacement can proceed.

02

Nonce verification on the AJAX request

Likewise, before processing, the replace action checks a nonce, the same protection WordPress uses throughout its own admin.

03

File type still validated

In addition, wp_check_filetype() still confirms the replacement is a permitted file type, rejecting a disallowed type regardless of what the original file was.

04

A visible warning for type mismatches

Even then, replacing a file with a different type still succeeds, but the visible warning means it never happens by surprise.

Verified compatibility

Built on the same native functions a fresh upload uses

Indeed, WordPress core provides the APIs this code integrates through, so this page makes no compatibility claim beyond the verified implementation.

Upload handling

wp_handle_upload()

Specifically, this is the same core function WordPress uses for every standard file upload, handling the raw incoming file before it moves into place.

wp_handle_uploadNative upload handling
Metadata regeneration

wp_generate_attachment_metadata()

Similarly, this is the exact function WordPress runs on a fresh upload to build every registered image size, applied here to the replacement file.

wp_generate_attachment_metadataNative regeneration
Cache-busting touchpoints

Seven native filters and functions

Together, wp_get_attachment_url, the_content, wp_prepare_attachment_for_js, wp_get_attachment_image_src, rest_prepare_attachment and wp_calculate_image_srcset cover a distinct way WordPress renders the URL.

7 native hooksComprehensive coverage
Solution comparison

TheOneWP versus common alternatives

Below, compare the verified Media Replace implementation with deleting and re-uploading a file manually.

CapabilityTheOneWP Media ReplaceOther common solutions
Keeping the same URL and ID Guaranteed by an in-place file overwriteDeleting and re-uploading always creates a new ID and address
Regenerating thumbnails Every registered size rebuilt automaticallyOften requires a separate manual regenerate step
Cache-busting coverage Seven distinct rendering paths, plus a direct DB stampFrequently limited to just the main URL, missing srcset and cached content
Aspect ratio changes Old thumbnail filenames found and updated site-wideCan leave broken thumbnail references after a ratio change
Administration One button per attachment, nothing else to configureUsually requires a dedicated plugin for equivalent reliability
Recommended workflow

Replace a file in four steps

Afterward, check the result on a page that actually embeds the file before considering it done.

01

Enable Media Replace

First, activate the module from the TheOneWP Content settings tab.

02

Open Replace File on the attachment

Then, use the row action in the media library, or the button on the attachment's own edit screen.

03

Drag in or select the replacement

Next, drop the new file into the dialog, or click to choose it, and confirm any type-mismatch warning that appears.

04

Confirm the replacement everywhere it's used

Finally, check a post or page that embeds the file to confirm the new version displays correctly.

Best practices

Confirm the result, not just the upload

Although doing the replacement itself is quick, verifying it landed everywhere is what actually makes it reliable.

01

Keep the same file type where possible

Since matching the original type avoids the warning entirely, it also keeps behavior most predictable.

02

Replace rather than delete and re-upload

Whenever a file just needs correcting, replacing it therefore preserves every existing reference automatically.

03

Check a cached or CDN-fronted page after replacing

If the site sits behind a page cache or CDN, it's also worth verifying the new file actually appears there too, since some external caching layers sit outside what this module can reach.

04

Confirm thumbnails after a significant aspect ratio change

Because a large change in aspect ratio is exactly the case where thumbnail filenames change, it's worth a quick visual check.

Common mistakes

Avoid overlooking what the warning is telling you

Nonetheless, a couple of details are easy to skip past when a replacement finishes quickly.

Ignoring the type-mismatch warning

Indeed, swapping in a different file type changes the content while the URL stays the same, which is exactly why the warning exists; read it before confirming.

Forgetting an external cache or CDN sits in front of the site

Although this module's cache-busting covers everything WordPress itself renders, a separate CDN layer may still need its own cache purge.

Expecting alt text and captions to carry over

Since they described the original file's content rather than the replacement's, alt text, description and caption get cleared on purpose.

Frequently asked questions

Media Replace FAQ

Overall, these answers come directly from the verified class, its JS asset and the settings registration.

What does Media Replace do?

Essentially, it overwrites the file behind an existing attachment, regenerating thumbnails and updating metadata, while keeping the same attachment ID and URL.

Does replacing a file break links or embeds pointing at it?

No. Since the URL and attachment ID never change, every post, gallery and direct link that already references the file keeps working.

What happens to the old thumbnails?

Old thumbnails get deleted and then regenerated fresh from the new file, matching every image size currently registered on the site.

Can I replace a file with a different file type?

Yes, though a warning appears first, since the URL stays the same while the actual content changes.

Will visitors see the old cached version after a replacement?

Specifically to prevent that, a version parameter gets stamped across every way WordPress renders the URL, including directly into stored post content.

What happens to alt text and captions after a replacement?

They get cleared, since they described the original file's content rather than whatever replaces it.

Does this affect a CDN or external caching layer in front of the site?

Although the cache-busting covers everything WordPress itself renders, a separate CDN may still need its own cache purge.

What happens if the new file has a different aspect ratio?

In that case, the module finds and updates any thumbnail filenames that change as a result, everywhere they were referenced.

Who can replace a file?

Only a user with upload permission and edit access to that specific attachment.

Is there anything to configure beyond enabling the module?

No. The Replace File button appears automatically on every attachment once the module is on.

Stop deleting and re-uploading to fix one file.Replace it, and let every link keep working.

Overall, use Media Replace to swap a file in place, with thumbnails regenerated and every reference across the site staying intact.