Skip to content

Instantly share code, notes, and snippets.

@MyListingClub
Last active March 28, 2021 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MyListingClub/3e1a7c6205ac70a011f6b10f9e9ce090 to your computer and use it in GitHub Desktop.
Save MyListingClub/3e1a7c6205ac70a011f6b10f9e9ce090 to your computer and use it in GitHub Desktop.
functions.php | Limit Image Upload Size
//// 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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment