I am searching online and all I can see is how to increase file size for media upload, which I know how to do with php.ini
, but what I need to do is limit the file size for media upload only.
The client and his associates have trouble with understanding: Please do not upload images that are bigger than 1MB because your site will load forever.
They keep uploading images that are over 8 MB in size, and the whole site takes over 30 sec to load. It’s horrendous.
So I was thinking – if it’s possible to limit the image upload to 1 MB or so without affecting the general upload_max_filesize
which will influence the ability to upload themes and plugins (and I don’t want that to happen).
Any idea if this can be done?
4 s
Yes, you can use the wp_handle_upload_prefilter
that allows you to stop the uploading process if a specific condition is not accomplished.
In your case, you could try this code snippet:
function limit_upload_size( $file ) {
// Set the desired file size limit
$file_size_limit = 1024; // 1MB in KB
// exclude admins
if ( ! current_user_can( 'manage_options' ) ) {
$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 limit is %d KB.' ), $file_size_limit );
}
}
return $file;
}
add_filter ( 'wp_handle_upload_prefilter', 'limit_upload_size', 10, 1 );
Hope it helps!