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.

Somebody knows some trick in Add new Post to:

  1. Disable the upload of audio, video and other filetypes.

  2. Only accept the upload of an image (jpg, png, gif).

  3. Limit the upload of each Post to only One image (no more than one).

Thanks in advance.

share|improve this question
1  
You can also modify the Flash uploader to allow only one file. This works together with the solution Mike gave. –  Jan Fabry Apr 26 '11 at 14:07
add comment

1 Answer 1

up vote 16 down vote accepted

Hi José Pablo Orozco Marín:

I was about to give up thinking that it wasn't possible or at least easy and then I stumbled onto the wp_handle_upload_prefilter filter which gives you exactly what you asked for! Here's the code:

add_filter('wp_handle_upload_prefilter', 'yoursite_wp_handle_upload_prefilter');
function yoursite_wp_handle_upload_prefilter($file) {
  // This bit is for the flash uploader
  if ($file['type']=='application/octet-stream' && isset($file['tmp_name'])) {
    $file_size = getimagesize($file['tmp_name']);
    if (isset($file_size['error']) && $file_size['error']!=0) {
      $file['error'] = "Unexpected Error: {$file_size['error']}";
      return $file;
    } else {
      $file['type'] = $file_size['mime'];
    }
  }
  list($category,$type) = explode('/',$file['type']);
  if ('image'!=$category || !in_array($type,array('jpg','jpeg','gif','png'))) {
    $file['error'] = "Sorry, you can only upload a .GIF, a .JPG, or a .PNG image file.";
  } else if ($post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false)) {
    if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>0)
      $file['error'] = "Sorry, you cannot upload more than one (1) image.";
  }
  return $file;
}

And here are some screenshots showing how it looks in action:

Screenshot of WordPress Upload Dialog with code to disable multiple uploads of anything besides image files

Screenshot of WordPress Upload Dialog with code to disable multiple image uploads per post

share|improve this answer
    
Thanks Mike. Seems to work on non flash upload process, but using flash version shows: ... Sorry, you can only upload a .GIF, a .JPG, or a .PNG image file. –  José Pablo Orozco Marín Jan 30 '11 at 16:17
    
@José Pablo Orozco Marín - Thank you for finding my error. I think I've found the problem and posted a newer version of the code that I do believe works for the flash uploader now. Let me know! –  MikeSchinkel Jan 31 '11 at 2:35
    
Works like a charm. Thanks. –  José Pablo Orozco Marín Feb 5 '11 at 17:37
    
@José Pablo Orozco Marín - Awesome. Glad I could help. –  MikeSchinkel Feb 5 '11 at 17:42
    
where should I add this line - and is it compatible tu wp 3.01 and latter ? –  user3028 Feb 9 '11 at 10:37
show 6 more comments

protected by Community Aug 5 '11 at 9:54

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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