0

I would like to put a different limitation on uploading content in the media library ( depending on file size and file type)

For example :

  • Upload mp4 (allow maximum upload file size 64mo)
  • Upload pdf (allow maximum upload file size 30mo)
  • Upload png/jpeg (allow maximum upload file size 15mo)
  • Upload doc/xls/ppt (allow maximum upload file size 25mo)

Thanks in advance for your help.

1 Answer 1

0

You can try to use the next code:

// NAV: Different upload file size limits based on file type
function wpcoder_upload_files_limits($file) {
    // Define custom file size limits based on file type
    $file_size_limits = array(
        'mp4' => 64 * 1024 * 1024, // 64 MB
        'pdf' => 30 * 1024 * 1024, // 30 MB
        'png' => 15 * 1024 * 1024, // 15 MB
        'jpeg' => 15 * 1024 * 1024, // 15 MB
        'doc' => 25 * 1024 * 1024, // 25 MB
        'xls' => 25 * 1024 * 1024, // 25 MB
        'ppt' => 25 * 1024 * 1024, // 25 MB
    );

    $file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    
    // Check if the file type has a custom size limit
    if (isset($file_size_limits[$file_ext])) {
        $max_file_size = $file_size_limits[$file_ext];
        
        // Check if the uploaded file exceeds the custom size limit
        if ($file['size'] > $max_file_size) {
            $file['error'] = 'File size exceeds the allowed limit for ' . esc_attr($file_size_limits[$file_ext]). 'KB ' . esc_attr(strtoupper($file_ext));
        }
    }

    return $file;
}

add_filter('wp_handle_upload_prefilter', 'wpcoder_upload_files_limits');

You can insert this code in the footer.php or use the free plugin WP Coder and insert this code in 'Global PHP'

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.