I found this code posted by: MikeSchinkel some time ago have a question how to change this code that i will be able to upload max 5 pictures insteed of 1 ? :)

    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;
  • Welcome to WPSE. Please look into How to Ask. This is no WordPress question, just a PHP one and will be better asked on SO.
    – kaiser
    Jan 27, 2014 at 17:26
  • You might get a little more help if you also link to where you found the code you posted, to add some context. Jan 27, 2014 at 17:28
  • You're better off adding a comment to the original question asking for that option; this appears to be it: wordpress.stackexchange.com/questions/8107/… Jan 27, 2014 at 17:37
  • tnorthcutt Hey i have found it in here wordpress.stackexchange.com/questions/8107/… songdogtech yes but it says You must have 50 reputation to comment
    – c64girl
    Jan 27, 2014 at 17:49
  • Anyone ? Maybe someone know some plugin that allow only upload images and some amount of them pre user ?
    – c64girl
    Jan 27, 2014 at 21:51

1 Answer 1


I haven't tested this but I believe you would simply change some of the code close to the end of the snippet you posted.

if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>5)
  $file['error'] = "Sorry, you cannot upload more than five (5) images.";

Let me know if this doesn't address your needs.

  • Sorry the code not working. You can upload more than 5 images. Wow i can post now...
    – c64girl
    Feb 3, 2014 at 16:49
  • Well, given your email I tried to help with the limited time I have today. Sorry this didn't do it. Feb 3, 2014 at 17:10
  • Ok no problem can You try again please :) If You have a bit of free time .
    – c64girl
    Feb 4, 2014 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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