Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is an issue with wordpress's month based organization of media files.

To re-create the issue:

  1. Delete an image file from a previous month
  2. Upload the same image file (same name)
  3. The newly uploaded file gets placed in the SAME month folder as it was originally, rather than in the current month.

This is an issue for me because the amazon s3 plugin (code.google.com/p/wordpress-s3/) which I'm using to host my images on s3, DOES place the newly uploaded file in the current month, so the file on s3 is in a different location than on the wordpress server.

Does anyone have experience with this "bug"? Which is the correct behavior, that of wordpress, or the s3 plugin?

thanks!


Some clarification:

What it looks like is that the s3 plugin and wordpress DO NOT work in parallel in their creation of month based uploads folders.

s3 always uses the current month. In other words, regardless of the publish date of the post, the upload will always get placed in the current month folder (i.e. if today's date is may 2012, I upload an image and it goes to s3bucket/uploads/2012/05/)

By contrast, it seems as though wordpress places the images in the folder based on the date of the original creation of the post. So if you create a post in february save as a draft, then add images in may, then publish it, in s3 your images will be in 2012/05, while in your regular server they will be in 2012/02. This is a serious problem for me for a lot of reasons that are too complex to get into here, but it is a serious issue nonetheless.

share|improve this question
1  
You deleting image manually via ftp or via wordpress? –  Tommixoft May 23 '12 at 19:08
    
deleting via wordpress –  j-man86 May 23 '12 at 20:58

1 Answer 1

Something you can try, but I haven't tested it throughly, and you'll need to do some QA to make sure it doesn't ripple out and affect anything else. Explanation:

The function that returns paths to the upload directory is called wp_upload_dir(). It's used in a bunch of places (and by a lot of plugins) to generate, er, a path to the upload dir. It accepts a parameter called time, which (when passed) will dictate which year/month gets used in the dir structure.

Unfortunately, when uploading from a post, there's nowhere to filter this "time". The function that does the uploading (called media_handle_upload(), I'm pretty sure), always uses the publish date of the post when media is being uploaded from a post edit screen. It only uses the current time if the upload isn't associated with a post. No filters there.

But! The wp_upload_dir() function has a filter you can use, named "upload_dir". It filters an array containing all the parts of the generated dir. One of the array items is named "subdir", and it contains the year/month part of the path, if applicable. You can use this filter to check if the subdir portion is non-empty, and if it is, replace its value with the current year/month.

The gamble there is that you can't tell the context in which wp_upload_dir is being called, and you're betting that no other functions actually use the time parameter in a way that'd break. I took a quick look around the core, and the only place I see it used is in the function wp_upload_bits, and I'm not sure exactly what that's for. In any case, I'd guess it only gets called on actual file uploads, so you're probably good there. But you'll want to test thoroughly with the plugins you have installed.

Code would look something like this:

    function wpsx_53067_filter_upload_dir($arr) {

        if(isset($arr['subdir']) && !empty($arr['subdir'])) {

            // The existing dir, for reference
            $old_dir = $arr['subdir'];

            // Your new dir (you could edit this to grab the current year/month)
            $new_dir = '/any-new-dir-you-like';

            // Update the array. Need to update the subdir, path and url items (they all contain the full path)
            $arr['subdir'] = $new_dir;
            $arr['path'] = str_replace($old_dir, $new_dir, $arr['path']);
            $arr['url'] = str_replace($old_dir, $new_dir, $arr['url']);

        }

        return $arr;

    }
   add_filter('upload_dir', 'wpsx_53067_filter_upload_dir');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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