I need my authors to have the right to delete any of the attachments they uploaded in case they made some mistake. Currently if the author clicks on any thumbnail in media-library, he gets the edit_image
link but not the delete_permanently
link ( as it appears for higher roles ). So right now, the author gets to edit the text content and edit the image but cannot delete the image-attachment.
I took a look at wordpress codex for roles and capabilities and found that there is nothing called delete_attachments. So is this possible, What am I missing ? I am trying to employ a bit of code something like the one suggested here for the add_cap filter.
example code :
function add_author_caps() {
$role = get_role( 'author' );
$role->add_cap( 'delete_attachments' ); //of course this wont work
}
add_action( 'admin_init', 'add_author_caps');
How do I get something like this to work. Again, basically I am just looking for a way for my site-authors to have a capability to delete the attachments they uploaded.
It seems that you have to add the capability yourself. You can get the necessary code for that in How do I create a custom role capability?.
You can also use Members Plugin which seems to do that for you(I haven’t used it myself yet). I think this discussion here will also help you to find the right direction.
EDIT: I haven’t really worked with author before. I tried to see what they can do and surprisingly they can delete their own Media uploads. So, there must be something wrong with your installation. Maybe some plugin has removed their ability to do that.
I then checked all the capabilities the author of my wordpress dev setup has. My author role has following capabilities
upload_files
edit_posts
edit_published_posts
publish_posts
read
level_2
level_1
level_0
delete_posts
delete_published_posts
I used the following code to check the capabilities.
You should check which capabilities your author have. You can compare it with the capabilities of author. Then you can add the missing capability with the code you have posted.
Here is how to allow edition and deletion of attachments for owned ones only to a custom role.
Add the capabilities ‘manage_other_users_attachments’ and ‘manage_own_attachments’ to the admin role, and the capability ‘manage_own_attachments’ to the custom role.
Then add this to the map_meta_cap filter :
It’s also a good idea to hide attachments that are not owned by the custom role (this code is borrowed from WPB website) :
I spent a while researching that so I thought I’d spare the trouble to other devs.
Laura
You can allow the Author role (or any custom role) in WordPress to delete Media Library items with the following snippet:
The capability to edit/delete attachment items in the WordPress Media Library is
edit_posts
anddelete_post
as shown in WP_Media_List_Table::_get_row_actions():However, one challenge with WordPress sharing the
edit_posts
anddelete_posts
capability for Posts and Media is granting the ability to manage both, even if only one is intended.For example, if you want Authors to delete and edit Media, they’ll be opted in to accessing the Posts and Comments admin menus — which may not be desirable.
One approach is to cleverly hide those Admin Menu links from them using the following:
Keep in mind this won’t revoke their access to the Posts or Comments screens, they just won’t see the admin menu in the Dashboard.
Naturally, a more robust approach should be used to control their access, but this purpose of this is to demonstrate the potentially unwanted behavior when adding the
edit_posts
ordelete_posts
to a user role in WordPress.