How can you limit the number of images / videos that can be uploaded to a WordPress post

there will be lot of users on my site who will be author uploading their posts. right now, they can upload as many images / videos per post as they want. i want to restrict them to upload maximum 4 images / videos per post. can anybody help me please ?

3 Answers
3

try this:

add_filter('wp_handle_upload_prefilter', 'limit_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'];
    }
  }
  if ($post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false)) {
    if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>3)
      $file['error'] = "Sorry, you cannot upload more than four (4) image.";
  }
  return $file;
}

after you paste this in your theme’s functions.php the upload limit should be 4 files total per post.

Leave a Comment