MyListing Club

Limit Image Upload Size

Building better MyListing websites. One code snippet at a time.

Overview

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 we recommend that you put this snippet in place before you launch your website.

This code snippet limits the max file upload size to 600KB.

Implementation

  1. Grab the code snippet.
  2. Follow the provided instructions.
  3. Save your changes.
				
					//// INSTRUCTIONS: 
//// Copy and paste the code snippet below into the child theme's functions.php file.
//// You can edit the functions.php file from your WordPress dashboard (Appearance > Theme Editor) or via FTP.

//// NOTES:
//// Adjust each instance of "600" (i.e. 600KB) value as desired.
//// Adjust each instance of "600KB" for a cosmetic-only reference that matches your actual file size limit.

--------------------------- CODE SNIPPET IS BELOW THIS LINE --------------------------- 

// LIMIT IMAGE UPLOAD SIZE
function max_image_size( $file ) {
  $size = $file['size'];
  $size = $size / 600;
  $type = $file['type'];
  $is_image = strpos( $type, 'image' ) !== false;
  $limit = 600;  
  $limit_output = '600KB';
  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' );