// min image dimensions warning
add_action('admin_init', function(){
add_settings_field(
'min_width',
'Minimal image upload width',
'min_width_fn',
'media'
);
register_setting(
'media',
'min_width'
);
function min_width_fn(){
echo('<input id="min_width" name="min_width" type="number" value="' . get_option('min_width') . '" />');
}
add_settings_field(
'min_height',
'Minimal image upload height',
'min_height_fn',
'media'
);
register_setting(
'media',
'min_height'
);
function min_height_fn(){
echo('<input id="min_height" name="min_height" type="number" value="' . get_option('min_height') . '" />');
}
});
add_filter('wp_handle_upload_prefilter','duuuda_handle_upload_prefilter');
function duuuda_handle_upload_prefilter($file)
{
if ( $file['error'] != '0' || empty(get_option('min_width')) || empty(get_option( 'min_height', '' ))) // there's already an error
return $file;
$img = getimagesize($file['tmp_name']);
$minWidth = get_option('min_width');
$minHeight = get_option('min_height');
$width = $img[0];
$height = $img[1];
if ($width < $minWidth || $height < $minHeight){
$file['error'] = sprintf( __( 'Image dimensions are too small. Minimum image size is %1$sx%2$spx. Uploaded image size is %3$sx%4$spx.' ), get_site_option('min_width'), get_site_option('min_height'), $width, $height );
"Image dimensions are too small. Minimum image size is {$minWidth}x{$minHeight}px. Uploaded image size is {$width}x{$height}px";
}
return $file;
}
<< back