Block editor: How stop media upload if user has already uploaded too many files?
-
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 ??
- The topic ‘Block editor: How stop media upload if user has already uploaded too many files?’ is closed to new replies.