add capability to author role to be able to delete attachments

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.

3 Answers
3

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.

$role = get_role('author');
foreach( $role->capabilities as $name => $val ){
    echo $name . "<br />";
}

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.

Leave a Comment