How to swap files in the WordPress Media Library

2 mins

The Media Library in WordPress makes it easy to quickly upload all manner of files to your website with a simple drag and drop. However, one gap in functionality on a standard WordPress install is the ability to directly swap an existing file for a new version.

Often it can be desirable to keep the same file name, particularly in the case of something like a PDF or image that may be indexed by Google or referenced by other external sources. Change the file name and visitors (including the Google search crawler) will get a 404 not found error next time they come knocking.

Swapping in a new version of a file without changing its link is possible, but in a standard WordPress install it does involve jumping through a few hoops.

You can either…

1) Note down the existing file name, give your new file that exact same name, then delete the old one and upload the new one in its place. If you are careful and quick this approach works perfectly well, but there is scope for errors to creep in and it won’t work if your Media Library is set to store uploads in date-based folders. In that case uploading on a different day will result in a different url even if the file name is the same as something like yourwebsite.com/wp-content/2019/07/filename could become yourwebsite.com/wp-content/2019/08/filename

2) Connect to your web server via FTP or similar and directly overwrite the file. This method neatly side-steps any issues around date-based file structures, but does have some drawbacks. For example, when replacing an image directly via FTP WordPress won’t automatically re-generate all the associated thumbnail versions that get created when uploading via the Media Library. And of course when digging through the full WordPress file structure there is also the possibility of accidentally deleting or moving an essential website file.

Luckily that’s not where this article ends. There is a another way.

One of the great things about WordPress is the rich ecosystems of plugins out there to extend what it can do out of the box.

A great little plug-in called Enable Media Replace that does just that. With it installed now you can select an existing file in the media library, click on the new Replace Media option in the Attachment Details window and upload a new version to take its place. You don’t even need to worry about giving your new file the same name, as long as you are swapping like-for-like – i.e a PDF for another PDF, the plug-in will handle everything for you.

We loved this neat little add-on so much that we have now made it one of the standard set of plug-ins that we include on new WordPress installations, along with others that work inside the Media Library uploader to handle things like image optimisation or automatically transferring media to content delivery networks.