Saya lebih suka tidak memformat ulang kode kolega.
Jadi, ini adalah jawaban yang hampir sama dengan @ MaorBarazany, tetapi memeriksa tipe mime, mengubah file['error']
deklarasi dan mengubah fungsi namespace ke wpse Question ID ini.
Selain itu, pemeriksaan hanya terjadi untuk pengguna yang bukan administrator .
add_action( 'admin_init', 'wpse_28359_block_authors_from_uploading_small_images' );
function wpse_28359_block_authors_from_uploading_small_images()
{
if( !current_user_can( 'administrator') )
add_filter( 'wp_handle_upload_prefilter', 'wpse_28359_block_small_images_upload' );
}
function wpse_28359_block_small_images_upload( $file )
{
// Mime type with dimensions, check to exit earlier
$mimes = array( 'image/jpeg', 'image/png', 'image/gif' );
if( !in_array( $file['type'], $mimes ) )
return $file;
$img = getimagesize( $file['tmp_name'] );
$minimum = array( 'width' => 640, 'height' => 480 );
if ( $img[0] < $minimum['width'] )
$file['error'] =
'Image too small. Minimum width is '
. $minimum['width']
. 'px. Uploaded image width is '
. $img[0] . 'px';
elseif ( $img[1] < $minimum['height'] )
$file['error'] =
'Image too small. Minimum height is '
. $minimum['height']
. 'px. Uploaded image height is '
. $img[1] . 'px';
return $file;
}
Hasil kait: