function limit_jpg_png_upload_size( $file ) {
// Get the file type
$file_type = $file['type'];
// Define the maximum file size (in bytes)
$max_size = 500000; // 500 KB for example
// Check if the file is a JPG or PNG
if ( in_array( $file_type, array( 'image/jpeg', 'image/png' ) ) ) {
// Check the file size
if ( $file['size'] > $max_size ) {
// Set an error message
$file['error'] = 'The uploaded file exceeds the size limit of 500 KB for JPG and PNG images.';
}
}
return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'limit_jpg_png_upload_size' );