Mass re-uploading of media with WordPress and Cache Images

For quite a while in the past, I was in the bad habit of uploading the media associated with my posts in a separate storage directory on the server. I didn’t even bother to categorize it much, just dumped the stuff in ad-hoc subdirectories and forgot about it.

Over time, the storage directory became unmaintainable as stuff kept piling in and it was difficult to keep track of which pages each file was associated with. At the same time the built-in media handling in WordPress became quite efficient and accessible. So I switched to using that instead.

But there was still the problem of past media, laying around in the old storage directory, uncategorized, linked to in god knows where. I began to manually fix old posts referring to the storage, but there were dozens and dozens of pages worth of posts to fix individually, so I soon grew tired.

I was planning to write some one-time code to do the per-post re-uploading, when I discovered Cache Images. I quickly realized I could exploit it to my benefit in this case: using another alias for my server I could make Cache Images re-upload each file for me.

I began with individual files to see if everything would work as planned. Turns out there was a slight cosmetic error in the upload: the files would go into the current month’s upload directory instead of the directory corresponding to the old post’s date. This was bug #16777 in WordPress’ tracker, and there was a diff file I could patch my WordPress installation with. Luckily, the patch worked just fine and now the images each went to their respective monthly directories.

There’s still the issue of WP Smush.it not working properly. I’m told that Yahoo! are in the process of switching servers, which should hopefully fix the errors I’m getting, but in the meantime I’ve disabled smushing altogether. It’s too bad, it would’ve been good to get the pictures smushed en masse while re-uploading.