• Hello,
    I want to limit “number of files” and “file size” when uploading certain users (‘customer’) at once using WordPress media.
    For example, a maximum of 3 files should be uploaded at a time, and each file should be a maximum of 5 mb.
    In accordance with this scenario I solved the size issue but not the number of files. How can I limit the number of files?
    I’ve looked at these resources for this, but they didn’t work. Some are old and obsolete, and some are left unanswered.

    Source-1
    Source-2
    Source-3

    My code:

    function customer_limit_upload_size( $file ) {
         //Get current user    
         $user = wp_get_current_user(); 
    	 //Obtaining the role 
    	 $roles = ( array ) $user->roles; 
    	 
        // Set the desired file size limit
        $file_size_limit = 1024; // 1MB in KB
    
        // exclude admins
        if ( ! current_user_can( 'manage_options' )  && $roles[0] === 'customer') {
    
            $current_size = $file['size'];
            $current_size = $current_size / 1024; //get size in KB
    
            if ( $current_size > $file_size_limit ) {
                $file['error'] = sprintf( __( 'ERROR: File size must be no more than 1 MB.' ), $file_size_limit );
            }
    
        }
    
        return $file;
    
    }
    add_filter ( 'wp_handle_upload_prefilter', 'customer_limit_upload_size', 10, 1 );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Limiting the Number and Size of Files’ is closed to new replies.