This answer describes a hack in which it is possible to upload new media to an "old" folder like 2012/12. That hack no longer seems to work in recent WP versions. Meanwhile, all the plugins I can find for media/folder management seem to want to create an entirely new folder hierarchy, and bypass the native WordPress year/month system completely.
I just want to upload some lost media from original files outside WordPress into an "old" year/month folder associated with the publication date of an old article long since published, but missing its media somehow. I'd much prefer that to linking from within the old article to some new location, because that is just going to raise questions from some people.
So far my only option seems to be to downgrade WordPress and try to find a version that supports that "hack" in the link above. Is there a better way?
This answer describes a hack in which it is possible to upload new media to an "old" folder like 2012/12. That hack no longer seems to work in recent WP versions. Meanwhile, all the plugins I can find for media/folder management seem to want to create an entirely new folder hierarchy, and bypass the native WordPress year/month system completely.
I just want to upload some lost media from original files outside WordPress into an "old" year/month folder associated with the publication date of an old article long since published, but missing its media somehow. I'd much prefer that to linking from within the old article to some new location, because that is just going to raise questions from some people.
So far my only option seems to be to downgrade WordPress and try to find a version that supports that "hack" in the link above. Is there a better way?
Share Improve this question asked Jun 6, 2021 at 8:00 omataiomatai 1131 silver badge7 bronze badges2 Answers
Reset to default 4Okay, so here's a hack to get what you want. There are a few caveats though, so please be sure to take account of this:
- this is untested with WPMS. I haven't a clue how this will affect it.
- it uses a so-called "private" WordPress function, which means that the functionality of this function may change with any WP release and you can't rely on it to always work as you expect. So test it before you roll it out on any scale.
- I recommend using this and then removing it when you don't need it any longer, or if you leave it in-place, ALL your media will go to the same directory.
- code assumes PHP 5.3 and above.
- code assumes WordPress 4.5 and above.
- code style is not adhering to WordPress code styles for hooks as it's meant to be put in-place very temporarily and removed.
It works simply by filtering and forcefully replacing the uploads directory with the date you want.
add_filter( 'upload_dir', function () {
return _wp_upload_dir( '2021/01' );
}, 100, 0 );
How to use it?
- Put the code in your theme's
functions.php
- Simply replace
2021/01
with the YYYY/MM you want to store your images. - Test uploading an image to see that it works as you'd expect by placing it in the correctly dated folder.
- Upload your images
- Remove this code from your
functions.php
You're talking about 'lost' media. Do you mean that the posts that contain that media are still present and just the files are missing? i.e. the media is in the media library also, but it's just a 404 on the file?
You could then just upload the original image to the proper directory (check devtools for images with 404 and their file paths) with an (s)ftp client and then regenerate all the images in Wordpress with e.g. the Regenerate plugin