MyListing Club

Limit Image Upload Size

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


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.


  1. Grab the code snippet.
  2. Follow the provided instructions.
  3. Save your changes.
					//// INSTRUCTIONS: 
//// 1. Copy and paste the code snippet below into the child theme's functions.php file.
//// NOTE: You can edit the functions.php file from your WordPress dashboard (Appearance > Theme Editor) or via FTP.
//// 2. Adjust the '600' value (600 KB) on Line 15 as desired.
//// NOTE: '1024' is the equivalent of 1 MB.
//// 2. Adjust the '600' value on Line 16 to match the value on Line 15.
//// 3. Adjust Line 17 to match the value of Lines 15 and 16.
//// NOTE: This is what your users will see on the front-end, when adding or editing a Listing.

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

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' );