WordPress 限制上传图片的最小宽度和高度

作为一个开放注册的WordPress站点,为了保证文章的质量,你可能需要限制其他用户上传图片的最小宽度和高度。实现的方法也很简单,将下面的代码添加到当前主题的 functions.php 即可:

/**
 * WordPress 限制上传图片的最小宽度和高度
 * https://www.wpdaxue.com/wordpress-minimum-require-for-image-size.html
 */
add_action( 'admin_init', 'block_authors_from_uploading_small_images' );
 
function block_authors_from_uploading_small_images(){
	//除管理员以外,其他用户都限制
	if( !current_user_can( 'manage_options') )
		add_filter( 'wp_handle_upload_prefilter', 'block_small_images_upload' ); 
}
 
function block_small_images_upload( $file ){
	// 检测文件的类型是否是图片
	$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'] = 
			'图片太小了,最小宽度是 ' 
			. $minimum['width'] 
			. 'px,当前上传的图片宽度是 ' 
			. $img[0] . 'px';
 
	elseif ( $img[1] < $minimum['height'] )
		$file['error'] = 
			'图片太小了,最小高度是 ' 
			. $minimum['height'] 
			. 'px,当前上传的图片高度是 ' 
			. $img[1] . 'px';
 
	return $file;
}

该代码会对非管理员用户进行限制,如果上传的图片的最小宽度或高度达不到要求,就会限制上传和进行提示。效果类似于下图:

0572-wpdaxue_com

参考资料:http://wordpress.stackexchange.com/questions/28359/

来源:

https://www.wpdaxue.com/wordpress-minimum-require-for-image-size.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32392.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?