使WordPress投稿和订阅用户可以上传文件

Author: 陌小雨Date: 2020-09-01View: 131

给WordPress投稿者用户添加/移除上传文件权限代码分享如下:

添加权限:

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');
}

去除权限

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->remove_cap('upload_files');
}

从上面的代码可以看出:wordpress是上传文件的权限用的是"upload_files"