Our homepage is under construction, so all elements are in demo still

How To Allow a Contributor To Upload Media File In WordPress

October 11 2018

As we know that by default WordPress doesn’t allow a contributor to upload media files.
But an Author or Editor can upload media files in a WordPress site. You can surely upgrade an account from Contributor to Editor or Author.
This code is only for that time if you want to allow your contributor to upload media files. You can simply copy and paste the code functions.php file of your Theme.

// let’s check this user is contributor or not first
//we can also check this user has access to upload files or not
$user = wp_get_current_user();
 if ( in_array( ‘contributor’, $user->roles  ! current_user_can(‘upload_files’) ) {
   function webextent_allow_contributor_upload_files(){
    $role = get_role( ‘contributor’ );
    $role->add_cap( ‘upload_files’);
  }
  add_action( ‘admin_init’,  ‘webextent_allow_contributor_upload_files’ );
}

Note: This will apply to all of the contributors to your site.

If you would like to give access to a particular user then you can follow the next method.
We will use user ID and will match a particular user-ID with the current user id. If you don’t know how to collect a user ID then you can look at the below how I did it.

How to allow a contributor to upload media files ( only for particular users).

// let’s check this user is contributor or not first
//we can also check this user has access to upload files or not
$user = wp_get_current_user();
$user_id = 12; // collect the user id of the user
$current_user_id = $user->ID;
if ( in_array( ‘contributor’, $user->roles && $user_id == $current_user_id ) {
  function webextent_allow_contributor_upload_files(){
   $role = get_role( ‘contributor’ );
   $role->add_cap( ‘upload_files’);
 }
 add_action( ‘admin_init’,  ‘webextent_allow_contributor_upload_files’ );
}

How to collect a specific user ID.

If you would like to collect a specific user ID to allow a contributor to upload media files ( only for a specific user ). Then you can look at the below. It’s too easy.

First of all, you’ll need to go to users and then go to All users.  So if you would like to get any user ID what you’ll need is to hover on Edit button and you’ll see a link will appear.

It’ll be at the bottom of the page. So you’ll see there is a tag called ‘user_id’ and user ID will appear there. You’ll need to just take that ID. That’s it.

So collecting and User ID to allow a contributor to upload media file is to easy.

If you’re looking for to know more about WordPress default users and their permissions then you can look at it here: WordPress User Roles And Their Permissions

Tags:

0 comments

Leave a Reply

Mohammad Rahat
about me

Mohammad Rahat Tanjid

Mohammad Tanjid is a professional WordPress Developer and Designer, he builds WP Products like WP Themes/Plugins and author of webextent.net. He has been writing code for more than 5 years now.