Allow Contributors to Upload Files

I would like to allow contributors to upload a file. So I use this recommended plugin WP Role Editor After activated, I go to the plugin from User User Role Editor and then select contributor in the selection dropdown. After that I put a check on upload_files and hit update. Then, I login with contributor account to test uploading a file. Great, I see the media upload button but when I click to upload a file, I get this error:

Sorry, you are not allowed to attach files to this post

I posted a question about this in their support forums but the plugin author hasn't given a solution.

Now, I tried the code below that I've found in Google. However, it doesn't show the upload media button at all.

// Let Contributor Role to Upload 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' );
    }

So, what else can I do to get this done? Why doesn't WordPress allow Contributor role to upload a files? According to the theme I use, I have to set this role by default for register users if I want users to submit a post with approval.

Topic user-roles users Wordpress

Category Web


I used this code to make subscribers upload files and it works successfully.. I think you must add true in the last line.

if ( current_user_can('subscriber') && !current_user_can('upload_files') ) {

    add_action('init', 'allow_subscriber_uploads');

    function allow_subscriber_uploads() {
      $new_role = get_role('subscriber');
      $new_role->add_cap('upload_files', true);
     }
 }

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.