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 part two to this question: Post Specific Uploader

I am using an upload filter to put files in a location based on post_id. Now when the file uploads, the URL path in the meta is wrong and the image preview is broken (missing the post_id).

the URL shows as: http://foo.com/blog/wp-content/uploads/icon_jpeg.png but should (in this case) be: http://foo.com/blog/wp-content/uploads/571/icon_jpeg.png

This part is truly magic to me. Should the path be correct if those were the value when the file was uploaded? Is there a query param I need to change?

Thanks for helping a WP noob!

Cheers.Bo

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

untested, but it should work theoretically:

add_filter('get_attached_file', function($path, $file, $attachment_id){

  // get the post object of the current attachment
  $att = get_post($attachment_id);

  // prepend attachment post parent ID to the file name
  return substr_replace($path, "{$att->post_parent}/{$file}", -strlen($file));
});

Another filter, attempts to "fix" the path returned by WP's upload handler:

add_filter('wp_handle_upload', function($results){
  global $post;

  if(empty($post))
    return $results;

  extract($results);

  $uploads = wp_upload_dir();

  $file = str_replace($uploads['basedir'], "{$uploads['basedir']}/{$post->ID}", $file);
  $url = str_replace($uploads['baseurl'], "{$uploads['baseurl']}/{$post->ID}", $url);

  return compact('file', 'url', 'type');
});

This will not work if the media uploader doesn't expose the current post inside the global $post variable.

share|improve this answer
    
Ill give it a shot! So it looks as if i am grabbing the post var in transit? Here is the odd thing, if i close the result window, and open it again, the preview path is right, and the image IS there! –  roberthuttinger Apr 5 '12 at 3:31
    
before I give this a shot can you walk thru this or comment? does get_attached_file only affect the file path immediately after upload? .. sigh 15 rep to upvote... –  roberthuttinger Apr 5 '12 at 13:47
    
That function should be called every time you request an image from an attachment. It will get the file name of the image (which is a meta value of the attachment), then it appends it to the wp upload path to generate the full image path. What function are you using to display these images? –  onetrickpony Apr 5 '12 at 14:55
    
it only needs to be fixed immediately after the upload. Right after I upload, the uploader shows the image and the path, but they are both wrong. BUT, if I close the media uploader window, then reopen it, the path and the thumbnail do appear. I guess I could strstr() for the presence of the post_id and if not there, then alter. I think my question is, why is it happening only immediately after the download. Thanks for your help! –  roberthuttinger Apr 5 '12 at 15:19
    
This is because the uploader will update the current page based on the paths returned by the upload handler. I added another filter for that, it probably won't work though... –  onetrickpony Apr 5 '12 at 15:44
show 1 more comment

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.