admin管理员组

文章数量:1122846

I would like to allow contributors to upload media by using the following code added to functions.php. But the Add Media Button still doesn't show up.

// Allow Contributors to Add Media
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');
    }
}

I would like to allow contributors to upload media by using the following code added to functions.php. But the Add Media Button still doesn't show up.

// Allow Contributors to Add Media
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');
    }
}
Share Improve this question edited Dec 22, 2022 at 20:01 bueltge 17.1k7 gold badges61 silver badges97 bronze badges asked Jan 5, 2018 at 6:08 dekkapo khonjondekkapo khonjon 112 bronze badges 1
  • Does this answer your question? Allow contributor role to upload images and not edit already published articles – bueltge Commented Dec 22, 2022 at 20:05
Add a comment  | 

2 Answers 2

Reset to default 0

Try changing current_user_can('contributor') to current_user_can('edit_posts'). According to the Codex, 'Passing role names to current_user_can() is discouraged as this is not guaranteed to work correctly.'

I would try surrounding your conditional expression in brackets, just to make sure it's getting parsed properly:

if ( current_user_can('edit_posts') && ! 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'); }

本文标签: Using WordPress’s addcap method to Allow Contributors to Upload Media is not Working