admin管理员组

文章数量:1323734

When uploading with video files to the Media Library, it's apparent that you need to increase the Max upload file size value. But this will be applied to all media types, including images and documents.

Is there any way or simple code that can set different size limits based on the file type/extension so that I can grant a large limit for videos, a small limit for images, and a different limit for documents?

I think WordPress should handle this natively because it's not logical to have the same limit for all file types. This was OK in the past when WordPress saw the light for the first time, but not in the current time.

When uploading with video files to the Media Library, it's apparent that you need to increase the Max upload file size value. But this will be applied to all media types, including images and documents.

Is there any way or simple code that can set different size limits based on the file type/extension so that I can grant a large limit for videos, a small limit for images, and a different limit for documents?

I think WordPress should handle this natively because it's not logical to have the same limit for all file types. This was OK in the past when WordPress saw the light for the first time, but not in the current time.

Share Improve this question edited Sep 15, 2020 at 13:56 Jaafar Abazid asked Sep 15, 2020 at 10:17 Jaafar AbazidJaafar Abazid 952 silver badges11 bronze badges 3
  • 1 There are already some good questions with answers that ask about restricting the dimensions and file size of images on the site, but recommendations are off topic here, asking for a plugin will get your question closed and shut down – Tom J Nowell Commented Sep 15, 2020 at 11:37
  • I'm asking for a way to have multiple limits for the Max upload file size field based on the uploaded file extension. Thank you@TomJNowell. – Jaafar Abazid Commented Sep 15, 2020 at 13:54
  • 1 I understood that, that's why I left a comment not an answer. Those questions and answers are directly related to your question, with significant overlap, and hold important information that will be needed to answer this question. They may be able to solve your question without any additional help. – Tom J Nowell Commented Sep 15, 2020 at 14:27
Add a comment  | 

1 Answer 1

Reset to default 2

This should work:

function max_video_size( $file ) {
  $size = $file['size'];
  $size = $size / 1024;
  $type = $file['type'];
  $is_image = strpos( $type, 'video' ) !== false;
  $limit = 750;
  $limit_output = '750kb';
  if ( $is_image && $size > $limit ) {
    $file['error'] = 'Video files must be smaller than ' . $limit_output;
  }
  return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'max_video_size' );

Rinse and repeat, changing video in the $is_image line to whatever filetype you want to specify, and even by altering the function, building all desired filetypes into the one filter.


FULL DISCLOSURE = the core of this answer is found in Set limit to media upload?

本文标签: media librarySet different max upload size limits based on file typeextension