13

How to Limit Image Upload Size on Your MyListing Website

Powered by WPCodeBox

Preserve MyListing website performance and disk storage by limiting image upload size. This code snippet helps govern image sizes, preventing massive uploads and ensuring optimal site health. Implement this essential snippet before launching your MyListing website to restrict image file uploads to a maximum of 600KB.


<?php 

/**
 * Author: MyListing Club
 * Author URI: https://mylisting.club
 */

function max_image_size( $file ) {
  $size = $file['size'];
  $size = $size / 1024;
  $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' );

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.