<button id="aaoqw"><object id="aaoqw"></object></button>
<tbody id="aaoqw"></tbody>
<rp id="aaoqw"><acronym id="aaoqw"></acronym></rp>
<dd id="aaoqw"><track id="aaoqw"></track></dd>
<li id="aaoqw"><acronym id="aaoqw"></acronym></li>
<button id="aaoqw"><acronym id="aaoqw"></acronym></button>

<li id="aaoqw"></li>
也想出現在這里?聯系我們

WordPress 限制不同用戶角色可上傳的文件類型及大小

  • 文章介紹
  • 升級版本
  • 評價&建議

開放注冊的WordPress站點,一般都會根據不同等級的用戶角色來賦予不同的權限。文件上傳功能就是一個比較常用的功能,那么,如何限制不同用戶角色可上傳的文件類型及大小呢?下面倡萌就來說說這個問題。

讓用戶擁有上傳文件的權限

默認情況下,有些用戶是不允許上傳文件的,你可以在主題的 functions.php 添加下面的代碼:

//允許用戶投稿時上傳文件
if ( current_user_can('contributor') && !current_user_can('upload_files') )
   add_action('admin_init', 'allow_contributor_uploads');
 
   function allow_contributor_uploads() {
      $contributor = get_role('contributor');
      $contributor->add_cap('upload_files');
}

上面的代碼就是給 'contributor' 這個用戶角色添加了 'upload_files' (上傳文件)的權限。

限制用戶上傳文件的類型

首先,大家可以先了解一下 WordPress 默認允許上傳的文件類型,打開WordPress的 /wp-includes/functions.php 文件,然后搜索 function wp_get_mime_types 定位到那里,你就會看到詳細的文件類型:

function wp_get_mime_types() {
	// Accepted MIME types are set here as PCRE unless provided.
	return apply_filters( 'mime_types', array(
	// Image formats
	'jpg|jpeg|jpe' => 'image/jpeg',
	'gif' => 'image/gif',
	'png' => 'image/png',
	'bmp' => 'image/bmp',
	'tif|tiff' => 'image/tiff',
	'ico' => 'image/x-icon',
	// Video formats
	'asf|asx|wax|wmv|wmx' => 'video/asf',
	'avi' => 'video/avi',
	'divx' => 'video/divx',
	'flv' => 'video/x-flv',
	'mov|qt' => 'video/quicktime',
	'mpeg|mpg|mpe' => 'video/mpeg',
	'mp4|m4v' => 'video/mp4',
	'ogv' => 'video/ogg',
	'mkv' => 'video/x-matroska',
	// Text formats
	'txt|asc|c|cc|h' => 'text/plain',
	'csv' => 'text/csv',
	'tsv' => 'text/tab-separated-values',
	'ics' => 'text/calendar',
	'rtx' => 'text/richtext',
	'css' => 'text/css',
	'htm|html' => 'text/html',
	// Audio formats
	'mp3|m4a|m4b' => 'audio/mpeg',
	'ra|ram' => 'audio/x-realaudio',
	'wav' => 'audio/wav',
	'ogg|oga' => 'audio/ogg',
	'mid|midi' => 'audio/midi',
	'wma' => 'audio/wma',
	'mka' => 'audio/x-matroska',
	// Misc application formats
	'rtf' => 'application/rtf',
	'js' => 'application/javascript',
	'pdf' => 'application/pdf',
	'swf' => 'application/x-shockwave-flash',
	'class' => 'application/java',
	'tar' => 'application/x-tar',
	'zip' => 'application/zip',
	'gz|gzip' => 'application/x-gzip',
	'rar' => 'application/rar',
	'7z' => 'application/x-7z-compressed',
	'exe' => 'application/x-msdownload',
	// MS Office formats
	'doc' => 'application/msword',
	'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
	'wri' => 'application/vnd.ms-write',
	'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
	'mdb' => 'application/vnd.ms-access',
	'mpp' => 'application/vnd.ms-project',
	'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
	'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
	'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
	'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
	'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
	'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
	'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
	'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
	'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
	'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
	'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
	'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
	'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
	'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
	'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
	'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
	'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
	'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
	'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
	'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
	// OpenOffice formats
	'odt' => 'application/vnd.oasis.opendocument.text',
	'odp' => 'application/vnd.oasis.opendocument.presentation',
	'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
	'odg' => 'application/vnd.oasis.opendocument.graphics',
	'odc' => 'application/vnd.oasis.opendocument.chart',
	'odb' => 'application/vnd.oasis.opendocument.database',
	'odf' => 'application/vnd.oasis.opendocument.formula',
	// WordPerfect formats
	'wp|wpd' => 'application/wordperfect',
	) );
}

=> 的前面為格式,后面為格式描述。如果你要禁止上傳其中的某些類型,可以參考下面的例子:

將下面的代碼添加到主題的 functions.php 文件:

//禁止上傳avi和mp4格式的文件

add_filter('upload_mimes', 'custom_upload_mimes');

function custom_upload_mimes ( $existing_mimes=array() ) {

unset ($existing_mimes['avi']);
unset ($existing_mimes['mp4']);

return $existing_mimes;

}

如果你還要禁止更多,可以按照 unset ($existing_mimes['格式']);  樣例添加即可。

如果你僅僅只需要允許用戶上傳幾種類型而已,還可以通過下面的更簡潔的方法,代碼添加到主題的 functions.php 文件:

//只允許上傳圖片文件
add_filter('upload_mimes', 'custom_upload_mimes');

function custom_upload_mimes ( $existing_mimes=array() ) {

unset ($existing_mimes);//禁止上傳任何文件

$existing_mimes['jpg|jpeg|gif|png']='image/image';//允許用戶上傳jpg,gif,png文件

return $existing_mimes;

}

如果你還要允許上傳其他格式,重復使用 $existing_mimes['格式']='描述';  即可。

限制用戶上傳的文件大小

同樣在主題的 functions.php 文件中,添加下面的代碼:

//限制上傳文件的最大體積
function max_up_size() {

return 500*1024; // 500 kb

}

add_filter('upload_size_limit', 'max_up_size');

上面的例子是限制所有用戶上傳的文件的最大體積為 500 kb (1M =1024*1024)。

注意:主機空間和WordPress本身一般設置了允許上傳的文件的最大體積,所以在這里設置需要考慮到這點。

限制不同用戶角色可上傳的文件類型及大小

其實上面已經給出了限制類型和大小的方法,要根據不同用戶角色來限制,只需要添加角色判斷代碼即可。倡萌舉個綜合的例子:

//不同用戶上傳的類型
function custom_upload_mimes ( $existing_mimes=array() ) {

	unset ($existing_mimes);//禁止上傳任何文件

	if( current_user_can( 'publish_posts' ) && !current_user_can( 'publish_pages' ) ) {

		//允許作者(Author)上傳的類型
		$existing_mimes['jpg|jpeg|gif|png']='image/image';//允許用戶上傳jpg,gif,png文件
		$existing_mimes['zip']='application/zip'; //允許用戶上傳zip壓縮包
		$existing_mimes['pdf']='application/pdf'; //允許用戶上傳pdf文件

	}elseif( current_user_can( 'edit_posts' ) && !current_user_can( 'publish_posts' ) ) {

		//允許投稿者(Contributor)上傳的類型
		$existing_mimes['jpg|jpeg|gif|png']='image/image';
		$existing_mimes['pdf']='application/pdf'; 

	}else{

		//其他用戶角色上傳的類型
		$existing_mimes['jpg|jpeg|gif|png']='image/image';

	}

	return $existing_mimes;

}

//不同用戶上傳的大小
function max_up_size() {

	if( current_user_can( 'publish_posts' ) && !current_user_can( 'publish_pages' ) ) {

		return 2048*1024; // 允許作者(Author)上傳 2M

	}elseif( current_user_can( 'edit_posts' ) && !current_user_can( 'publish_posts' ) ) {

		return 1024*1024; // 允許投稿者(Contributor)上傳 1M

	}else{

		return 500*1024; // 其他用戶角色上傳 500 kb

	}

}

//只對非管理員執行這兩個函數(即:對管理員不生效)
if( !current_user_can( 'manage_options' ) ) {

	add_filter('upload_mimes', 'custom_upload_mimes');
	add_filter('upload_size_limit', 'max_up_size');

}

大家只要靈活使用 if 語句判斷不同的角色賦予不同的權限即可

有用1
  • 2022.04.22初次和大家見面了!

等待您對該主題的建議

發表評論

還能輸入240個字

Hi, 歡迎加入Wordpress技術交流群,帶你裝逼帶你飛!

我要入群
也想出現在這里?聯系我們
wordpress加速

我來推薦一個更牛逼的給你看看?

  • 猛戳我吧
久久夜夜免费毛片
<button id="aaoqw"><object id="aaoqw"></object></button>
<tbody id="aaoqw"></tbody>
<rp id="aaoqw"><acronym id="aaoqw"></acronym></rp>
<dd id="aaoqw"><track id="aaoqw"></track></dd>
<li id="aaoqw"><acronym id="aaoqw"></acronym></li>
<button id="aaoqw"><acronym id="aaoqw"></acronym></button>

<li id="aaoqw"></li>