• ninsan

    (@jennyrigsjo)


    Good afternoon!

    I have the following piece of code in wp-config.php:

    /**
     * Custom max limit for the total size of a user's media library uploads (in binary bytes).
     *
     */
     define('ML_MAX_QUOTA', 3145728); //3 MB

    And in a mu-plugin file I have the following code:

    /**
     * Get the collective size in bytes of current user's uploaded media files.
     * @return int The total size of current user's media library uploads.
     */
    function jr_current_user_ml_quota() {
        //Calculate the total size of all media files uploaded by user and return the sum as an integer (actual code omitted for simplicity)
    }
    
    /**
     * Check how many bytes of data the current user has uploaded to the media library
     * and return an error message if the upload limit has been reached.
     */
    function jr_check_upload_limit_reached($file) {
    
        if (current_user_can('administrator') || !defined('ML_MAX_QUOTA')) {
            return $file;
        }
    
        $current_quota = jr_current_user_ml_quota();
        $new_quota = $current_quota + $file['size'];
    
        $upload_limit_reached = ($new_quota > ML_MAX_QUOTA);
    
        if ($upload_limit_reached) {
            $file['error'] = "File is too large. Delete one or more files to upload more.";
        }
    
        return $file;
    }
    
    add_filter('wp_handle_upload_prefilter', 'jr_check_upload_limit_reached');

    The above custom constant + filter callback works great to prevent users from uploading new files to the media library if they have exceeded the defined max quota. However, if a user tries to upload a file directly to a post from inside the block editor, the above code does not work as expected, but instead causes the upload process inside the editor to freeze/hang indefinitely (no error message is displayed).

    So my question is: how can I make the above code work properly inside the block editor? I’m guessing I would have to use Javascript? If anyone knows of any beginner-friendly tutorials on how to programmatically customize the block editor, please point me to them!

    Thanks.

    /jenny ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @jennyrigsjo

    I am confused between the title and the description.
    Do you want to limit image upload by total count or total size?

    Thread Starter ninsan

    (@jennyrigsjo)

    @kaavyaiyer By total size (sorry for the poorly worded title). I am interested in calculating the total, cumulative size of all of the current user’s uploaded media files and, if the total size exceeds the maximum quota, the user will need to free up space by deleting one or more files in order to upload new ones. The code I have so far works when a user uploads files to the media library, but it causes issues when they try to upload a file directly into a post within the block editor.

    Thanks for helping!

    /jenny ??

    Thread Starter ninsan

    (@jennyrigsjo)

    I just realized I submitted this post in the wrong forum. It should be in the ‘Developing with WordPress’ forum. Could someone move it for me? Thanks.

    /jenny

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Block editor: How stop media upload if user has already uploaded too many files?’ is closed to new replies.