我不想重新格式化同事的代码因此,这与@MaorBarazany的答案几乎相同,但检查mime类型,更改file[\'error\']
声明并将函数命名空间更改为此wpse问题ID。
此外,该检查仅针对以下用户进行not administrators.
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;
}
Result of the hook: