The Limit Image Upload Size code snippet helps preserve the performance and disk storage of a MyListing website.
If image governance is not performed, users can upload massive image sizes, so I recommend that you put this snippet in place before you launch your website.
This code snippet limits the max file upload size to 600kb and this is what your users will see/experience on the front-end when adding or editing a Listing.
Check out our Image Optimization for MyListing Websites guide for more information.
Customization Notes
‘1024’ is the equivalent of 1 MB.
- Adjust the ‘512’ value as desired.
- Adjust the ‘512kb’ value to match the value of the previous value.
// LIMIT IMAGE UPLOAD SIZE
function max_image_size( $file ) {
$size = $file['size'];
$size = $size / 1024;
$type = $file['type'];
$is_image = strpos( $type, 'image' ) !== false;
$limit = 512;
$limit_output = '512kb';
if ( $is_image && $size > $limit ) {
$file['error'] = 'Image files must be smaller than ' . $limit_output;
}//end if
return $file;
}//end max_image_size()
add_filter( 'wp_handle_upload_prefilter', 'max_image_size' );