0

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?

0

Okay, 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?

  1. Put the code in your theme's functions.php
  2. Simply replace 2021/01 with the YYYY/MM you want to store your images.
  3. Test uploading an image to see that it works as you'd expect by placing it in the correctly dated folder.
  4. Upload your images
  5. Remove this code from your functions.php
6
  • For the small amount of missing files I have, this looks like a good option - testing now :-)
    – omatai
    Jun 6 at 9:44
  • OK - this seems to work as advertised - images are certainly uploading to the correct folder and appearing in the Media Library. The only suspicious thing is that they are listed in the Media Library until 2021/06 and not in the 2012/12 that they were uploaded to. I imagine they will back up correctly... but I should test it.
    – omatai
    Jun 6 at 10:11
  • Yea, definitely worth testing. But if you're backing up files on disk, they should backup just fine. Glad it works and gets you most of the way there.
    – Paul G.
    Jun 6 at 10:16
  • 1
    I had a look to see if there was a simple way to change the media library dates as you upload it. You can't, but you could go into the database for the attachment post and change its date manually to be whatever you like. That's certainly an option. Always keep a back up handy incase of the worst.
    – Paul G.
    Jun 6 at 10:28
  • 1
    It's very straight forward: they're in the posts table, with post_type set to attachment and you simply edit the fields post_date_gmt and post_date.
    – Paul G.
    Jun 6 at 10:33
0

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

4
  • I guess it's a 404 - just an inline image in a post, but the link is broken (file not present in media library), so it shows as the correct size, but just blank. I think the site was reloaded from backup a couple of years ago when there was almost no media, and maybe the media was skipped from the backup completely. Either way, when I reloaded from backup recently, a few old files were not in the media library. I have the originals, so I'd prefer to restore them to the "old" location in the media library if that is possible.
    – omatai
    Jun 6 at 9:12
  • I can put all relevant files into 2012/12 easy enough... and make the links work again... but the Regenerate plugin (and the Media Library too) is oblivious to the existence of 2012/12 :-( So I'm missing something to truly inject the files into the Media Library... as opposed to simply uploading them into the folder structure used by the Media Library. How can I repair that missing database entry to get everything back in sync so that when I backup my site, I don't lose the files again?
    – omatai
    Jun 6 at 9:31
  • If it's inline images in content, you could also upload them as new media items and then replace the html from the few posts that have the missing inline images. Or if you don't want a newer year/month folder, then copy that newly generated images to the older folder and update the inline image in the html accordingly
    – Jos Faber
    Jun 6 at 9:39
  • As noted in my second comment, that restores the validity of the link... but the Media Library is not aware of this, and so I suspect the same files will be skipped when I do my next backup :-( It is easy to have EITHER restored links to "old" folder, OR edited links to "new" folder that appear in Media Libary and get backed up... but not Media Library that recognises artificially-created "old" location :-( Exploring the function.php hack alternative proposed - easier than downgrading Wordpress, that's for sure!
    – omatai
    Jun 6 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.