Yesterday I posted an article about allowing your WordPress website’ users to post on your website at this link. If you choose contributor role as the default role for your users, then you should know that the users with contributor roles are not allowed to upload images to their WordPress posts. If you want to allow contributors to upload images in WordPress, then here’s a little snippet for that.
Add following snippet to your current theme’s functions.php file:
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'); }
What do I need to do to install this to my functions.php file? Do I just paste it at the bottom of all the content? Also can I change ‘contributor’ to ‘subscriber’?
Add it to the bottom of your functions.php. And no, changing it to subscribers will not do anything.
I just tested this, and it worked perfectly. However, it gives the contributors the ability to delete their own images even after the posts are published, which isn’t a good idea in my opinion. Any workaround to deal with this?
Try adding the following code just above the last line:
$contributor->aremove_cap('edit_files');
I added it, but the contributors are still able to delete images from the media library. When a contributor accesses the media library, they still see the Edit | Delete Permanently | View links. They sure can’t edit the post itself though (which is the case with or without the $contributor->remove_cap(‘edit_files’); bit).
I found another snippet that does just that, I’ll put it here so people with the same problem will find a solution:
add_action('media_row_actions','users_own_attachments', 2, 1);
function users_own_attachments( $wp_query_obj ) {
if( !current_user_can( 'delete_plugins' ) ){
unset($wp_query_obj['delete']);
return $wp_query_obj;
}
Thank you, this article have been a great help!
Amazing! Hope it’s not causing any problem with the author role. I guess it will also take the access from any role which can’t delete the plugins, such as author.
Actually, and according to my test, the snippet hides the red “Delete Permanently” link for everyone, including admins (from Media Library). However, contributors still can select pictures they uploaded and bulk delete them through the bulk command list. So I guess the problem still persists, I was quick to the trigger there :D
Your code is missing a closing curly brace. Could cause a lot of heartache for people who don’t speak php.
I can’t see any missing curly brace at all. Please correct me if I’m wrong by pointing the line out :)
Thanks a lot, It works perfectly :)
Perfect — exactly what I needed. Thanks!
Thank you for this very useful code snippet, it worked!
Great known code.
Could you help us to activate the featured image for contributors without plugin ?
Thank you! Worked smoothly :)
Hey,
Its working bro, thank you for sharing.
Try User Role Editor Plugin. This will allow you to change many more settings.