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.

  • +1 and maybe you could attach the error to 'admin_notices'. :)
    – kaiser
    Commented Jun 26, 2011 at 16:12
  • @Bainternet - do you have any additional information or remarks on why you need to have that code for the flash uploader?
    – Josiah
    Commented Nov 22, 2011 at 14:38
  • 1
    @kaiser: i build also an solution for this, like the code on top; but I don't find an hook for admin notices in media upload, inside the iframe. Only an message on upload array is possible.
    – bueltge
    Commented Jun 21, 2012 at 13:48
  • @bueltge Ah, ok. Maybe custom js with default admin UI styles will be enough.
    – kaiser
    Commented Jun 25, 2012 at 10:14
  • its not fine for maintenance :( i have used current an hook for the information, which mime type and how many objects is possible. see: github.com/bueltge/Limit-Upload
    – bueltge
    Commented Jun 28, 2012 at 21:25

I have also on solution, use same idea and hooks, only more complex with more functionalities and structured in classes for easy reused. Was an customer project and now its free: https://github.com/bueltge/Limit-Upload

  • Firstly, your Adminimize plugin is amazing, secondly, is this available in the Wordpress plugin directory?
    – Christine Cooper
    Commented May 14, 2013 at 17:15
  • Yes, the Adminimize plugin is also available on the [wp Repo](wordpress.org/extend/plugins/adminimize/). The plugin Limit Upload is only on Github. The problem is the work with free plugin, to much for to give good support.
    – bueltge
    Commented May 17, 2013 at 15:49

Anyone wishing to use this code, be sure to include the 'posts_per_page' => -1 if you want to limit the number to more than 5 because the get_posts query on this page will only get a max of 5 because of the default paging.

see https://stackoverflow.com/questions/2134500/why-does-get-posts-return-only-5-matching-posts-when-it-should-return-9 for more information. Cheers!

  • 1
    Actually this would make the number un limited, not a limit greater than 5.
    – GhostToast
    Commented Nov 21, 2013 at 21:28

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.