所有分类
  • 所有分类
  • WordPress主题
  • WordPress插件
  • 源码
  • 软件
  • 主题教程

WordPress不同用户角色可上传的文件类型及大小限制

开放注册的WordPress站点,一般都会根据不同等级的用户角色来赋予不同的权限。文件上传功能就是一个比较常用的功能,那么,如何限制不同用户角色可上传的文件类型及大小呢?

让用户拥有上传文件的权限

默认情况下,有些用户是不允许上传文件的,你可以在主题的 functions.php 添加下面的代码:

  1. //允许用户投稿时上传文件
  2. if ( current_user_can(‘contributor’) && !current_user_can(‘upload_files’) )
  3.    add_action(‘admin_init’, ‘allow_contributor_uploads’);
  4.  
  5.    function allow_contributor_uploads() {
  6.       $contributor = get_role(‘contributor’);
  7.       $contributor->add_cap(‘upload_files’);
  8. }

上面的代码就是给 ‘contributor’ 这个用户角色添加了 ‘upload_files’ (上传文件)的权限。

限制用户上传文件的类型

首先,大家可以先了解一下 WordPress 默认允许上传的文件类型,打开WordPress的 /wp-includes/functions.php 文件,然后搜索 function wp_get_mime_types 定位到那里,你就会看到详细的文件类型:

  1. function wp_get_mime_types() {
  2.    // Accepted MIME types are set here as PCRE unless provided.
  3.    return apply_filters( ‘mime_types’, array(
  4.    // Image formats
  5.    ‘jpg|jpeg|jpe’ => ‘image/jpeg’,
  6.    ‘gif’ => ‘image/gif’,
  7.    ‘png’ => ‘image/png’,
  8.    ‘bmp’ => ‘image/bmp’,
  9.    ‘tif|tiff’ => ‘image/tiff’,
  10.    ‘ico’ => ‘image/x-icon’,
  11.    // Video formats
  12.    ‘asf|asx|wax|wmv|wmx’ => ‘video/asf’,
  13.    ‘avi’ => ‘video/avi’,
  14.    ‘divx’ => ‘video/divx’,
  15.    ‘flv’ => ‘video/x-flv’,
  16.    ‘mov|qt’ => ‘video/quicktime’,
  17.    ‘mpeg|mpg|mpe’ => ‘video/mpeg’,
  18.    ‘mp4|m4v’ => ‘video/mp4’,
  19.    ‘ogv’ => ‘video/ogg’,
  20.    ‘mkv’ => ‘video/x-matroska’,
  21.    // Text formats
  22.    ‘txt|asc|c|cc|h’ => ‘text/plain’,
  23.    ‘csv’ => ‘text/csv’,
  24.    ‘tsv’ => ‘text/tab-separated-values’,
  25.    ‘ics’ => ‘text/calendar’,
  26.    ‘rtx’ => ‘text/richtext’,
  27.    ‘css’ => ‘text/css’,
  28.    ‘htm|html’ => ‘text/html’,
  29.    // Audio formats
  30.    ‘mp3|m4a|m4b’ => ‘audio/mpeg’,
  31.    ‘ra|ram’ => ‘audio/x-realaudio’,
  32.    ‘wav’ => ‘audio/wav’,
  33.    ‘ogg|oga’ => ‘audio/ogg’,
  34.    ‘mid|midi’ => ‘audio/midi’,
  35.    ‘wma’ => ‘audio/wma’,
  36.    ‘mka’ => ‘audio/x-matroska’,
  37.    // Misc application formats
  38.    ‘rtf’ => ‘application/rtf’,
  39.    ‘js’ => ‘application/javascript’,
  40.    ‘pdf’ => ‘application/pdf’,
  41.    ‘swf’ => ‘application/x-shockwave-flash’,
  42.    ‘class’ => ‘application/java’,
  43.    ‘tar’ => ‘application/x-tar’,
  44.    ‘zip’ => ‘application/zip’,
  45.    ‘gz|gzip’ => ‘application/x-gzip’,
  46.    ‘rar’ => ‘application/rar’,
  47.    ‘7z’ => ‘application/x-7z-compressed’,
  48.    ‘exe’ => ‘application/x-msdownload’,
  49.    // MS Office formats
  50.    ‘doc’ => ‘application/msword’,
  51.    ‘pot|pps|ppt’ => ‘application/vnd.ms-powerpoint’,
  52.    ‘wri’ => ‘application/vnd.ms-write’,
  53.    ‘xla|xls|xlt|xlw’ => ‘application/vnd.ms-excel’,
  54.    ‘mdb’ => ‘application/vnd.ms-access’,
  55.    ‘mpp’ => ‘application/vnd.ms-project’,
  56.    ‘docx’ => ‘application/vnd.openxmlformats-officedocument.wordprocessingml.document’,
  57.    ‘docm’ => ‘application/vnd.ms-word.document.macroEnabled.12’,
  58.    ‘dotx’ => ‘application/vnd.openxmlformats-officedocument.wordprocessingml.template’,
  59.    ‘dotm’ => ‘application/vnd.ms-word.template.macroEnabled.12’,
  60.    ‘xlsx’ => ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’,
  61.    ‘xlsm’ => ‘application/vnd.ms-excel.sheet.macroEnabled.12’,
  62.    ‘xlsb’ => ‘application/vnd.ms-excel.sheet.binary.macroEnabled.12’,
  63.    ‘xltx’ => ‘application/vnd.openxmlformats-officedocument.spreadsheetml.template’,
  64.    ‘xltm’ => ‘application/vnd.ms-excel.template.macroEnabled.12’,
  65.    ‘xlam’ => ‘application/vnd.ms-excel.addin.macroEnabled.12’,
  66.    ‘pptx’ => ‘application/vnd.openxmlformats-officedocument.presentationml.presentation’,
  67.    ‘pptm’ => ‘application/vnd.ms-powerpoint.presentation.macroEnabled.12’,
  68.    ‘ppsx’ => ‘application/vnd.openxmlformats-officedocument.presentationml.slideshow’,
  69.    ‘ppsm’ => ‘application/vnd.ms-powerpoint.slideshow.macroEnabled.12’,
  70.    ‘potx’ => ‘application/vnd.openxmlformats-officedocument.presentationml.template’,
  71.    ‘potm’ => ‘application/vnd.ms-powerpoint.template.macroEnabled.12’,
  72.    ‘ppam’ => ‘application/vnd.ms-powerpoint.addin.macroEnabled.12’,
  73.    ‘sldx’ => ‘application/vnd.openxmlformats-officedocument.presentationml.slide’,
  74.    ‘sldm’ => ‘application/vnd.ms-powerpoint.slide.macroEnabled.12’,
  75.    ‘onetoc|onetoc2|onetmp|onepkg’ => ‘application/onenote’,
  76.    // OpenOffice formats
  77.    ‘odt’ => ‘application/vnd.oasis.opendocument.text’,
  78.    ‘odp’ => ‘application/vnd.oasis.opendocument.presentation’,
  79.    ‘ods’ => ‘application/vnd.oasis.opendocument.spreadsheet’,
  80.    ‘odg’ => ‘application/vnd.oasis.opendocument.graphics’,
  81.    ‘odc’ => ‘application/vnd.oasis.opendocument.chart’,
  82.    ‘odb’ => ‘application/vnd.oasis.opendocument.database’,
  83.    ‘odf’ => ‘application/vnd.oasis.opendocument.formula’,
  84.    // WordPerfect formats
  85.    ‘wp|wpd’ => ‘application/wordperfect’,
  86.    ) );
  87. }

=> 的前面为格式,后面为格式描述。如果你要禁止上传其中的某些类型,可以参考下面的例子:

将下面的代码添加到主题的 functions.php 文件:

  1. //禁止上传avi和mp4格式的文件
  2. add_filter(‘upload_mimes’, ‘custom_upload_mimes’);
  3. function custom_upload_mimes ( $existing_mimes=array() ) {
  4.    unset ($existing_mimes[‘avi’]);
  5.    unset ($existing_mimes[‘mp4’]);
  6.    return $existing_mimes;
  7. }

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

如果你仅仅只需要允许用户上传几种类型而已,还可以通过下面的更简洁的方法,代码添加到主题的 functions.php 文件:

  1. //只允许上传图片文件
  2. add_filter(‘upload_mimes’, ‘custom_upload_mimes’);
  3. function custom_upload_mimes ( $existing_mimes=array() ) {
  4.    unset ($existing_mimes);//禁止上传任何文件
  5.    $existing_mimes[‘jpg|jpeg|gif|png’]=‘image/image’;//允许用户上传jpg,gif,png文件
  6.    return $existing_mimes;
  7. }

如果你还要允许上传其他格式,重复使用 $existing_mimes[‘格式’]=’描述’;  即可。

限制用户上传的文件大小

同样在主题的 functions.php 文件中,添加下面的代码:

  1. //限制上传文件的最大体积
  2. function max_up_size() {
  3.    return 500*1024; // 500 kb
  4. }
  5. add_filter(‘upload_size_limit’, ‘max_up_size’);

上面的例子是限制所有用户上传的文件的最大体积为 500 kb (1M =1024*1024)。

注意:主机空间和WordPress本身一般设置了允许上传的文件的最大体积,所以在这里设置需要考虑到这点。

限制不同用户角色可上传的文件类型及大小

其实上面已经给出了限制类型和大小的方法,要根据不同用户角色来限制,只需要添加角色判断代码即可。倡萌举个综合的例子:

  1. //不同用户上传的类型
  2. function custom_upload_mimes ( $existing_mimes=array() ) {
  3.    unset ($existing_mimes);//禁止上传任何文件
  4.    if( current_user_can( ‘publish_posts’ ) && !current_user_can( ‘publish_pages’ ) ) {
  5.       //允许作者(Author)上传的类型
  6.       $existing_mimes[‘jpg|jpeg|gif|png’]=‘image/image’;//允许用户上传jpg,gif,png文件
  7.       $existing_mimes[‘zip’]=‘application/zip’; //允许用户上传zip压缩包
  8.       $existing_mimes[‘pdf’]=‘application/pdf’; //允许用户上传pdf文件
  9.    }elseif( current_user_can( ‘edit_posts’ ) && !current_user_can( ‘publish_posts’ ) ) {
  10.       //允许投稿者(Contributor)上传的类型
  11.       $existing_mimes[‘jpg|jpeg|gif|png’]=‘image/image’;
  12.       $existing_mimes[‘pdf’]=‘application/pdf’; 
  13.    }else{
  14.       //其他用户角色上传的类型
  15.       $existing_mimes[‘jpg|jpeg|gif|png’]=‘image/image’;
  16.    }
  17.    return $existing_mimes;
  18. }
  19.   
  20. //不同用户上传的大小
  21. function max_up_size() {
  22.    if( current_user_can( ‘publish_posts’ ) && !current_user_can( ‘publish_pages’ ) ) {
  23.       return 2048*1024; // 允许作者(Author)上传 2M
  24.    }elseif( current_user_can( ‘edit_posts’ ) && !current_user_can( ‘publish_posts’ ) ) {
  25.       return 1024*1024; // 允许投稿者(Contributor)上传 1M
  26.    }else{
  27.       return 500*1024; // 其他用户角色上传 500 kb
  28.    }
  29. }
  30.   
  31. //只对非管理员执行这两个函数(即:对管理员不生效)
  32. if( !current_user_can( ‘manage_options’ ) ) {
  33.    add_filter(‘upload_mimes’, ‘custom_upload_mimes’);
  34.    add_filter(‘upload_size_limit’, ‘max_up_size’);
  35. }

 

免责声明:
使用本站资源的用户均应仔细阅读本声明。用户使用本站资源的行为将被视为对本声明全部内容的认可。
1、本站资源大部分搜集于网络,整理并分享。用户可自行搜索在其他地方下载,也可选择在本站下载。如果侵犯了您的合法权益,请联系:diy945945@111.com 及时删除。本站资源仅用于研究、学习之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
2、本站对发布的资源不能保证其完整性、安全性和可用性。请您在下载后自行检查。您在使用过程中遇到的任何问题与本站无关。
3、注册本站以及在本站充值、开通会员等消费行为仅作为用户本人对本站的友情赞助,均为用户本人的自愿行为。相当于您是自愿赞助本站的服务器以及运营维护费用,而不是购买本站的任何服务与资源,请知悉!!!
4、本站资源资源采用网盘分享,如链接失效,请及时联系。
5、原文链接:https://www.xxziyuan.top/3403.html,转载请注明出处。
0

评论0

请先
M3U8视频批量下载PC工具,支持ts格式合并为MP4(v2.0.7)
M3U8视频批量下载PC工具,支持ts格式合并为MP4(v2.0.7)
9分钟前 有人购买 去瞅瞅看

站点公告

专注WordPress-源码-实用软件分享!

WordPress网站搭建联系qq:429413218

显示验证码
没有账号?注册  忘记密码?