1

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.

2

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.

5
  • Suggested first link confused me a bit as it seems to be more about plugin development and the discussion link assumes that the members plugin is installed. I am hoping for a solution with a minimal code, preferably with no extra bloat.
    – gurung
    Jan 6 '14 at 20:00
  • +1 for the Members plugin. It's lightweight and makes the job easy. If you didn't want the GUI you could easily take look at the plugin's code and remove the need for GUI.
    – Brooke.
    Jan 7 '14 at 7:46
  • @gurung I have edited my answer.
    – sakibmoon
    Jan 7 '14 at 9:48
  • @sakibmoon as you said, I found out that 'author' has the right to delete media attachments by default. Turns out, I had a plugin which denied the 'author' role to 'delete_posts.' I found this only by using your code. Thanks for all the help.
    – gurung
    Jan 8 '14 at 9:25
  • @gurung Glad I could help.
    – sakibmoon
    Jan 8 '14 at 9:38
5

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 :

if ( 'edit_post' == $requested_action || 'delete_post' == $requested_action ) {
    $post = get_post( $args[0] );
    $post_type = $post->post_type ;

    if ( 'attachment' == $post_type ){

        $required_caps = array();            
        $post_author_id = $post -> post_author;

        if( $user_id == $post_author_id ) {
            $required_caps[] = 'manage_own_attachments';     
        } else {                
            $required_caps[] = 'manage_other_users_attachments';
        }
    }
}

It's also a good idea to hide attachments that are not owned by the custom role (this code is borrowed from WPB website) :

add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments' );

 function wpb_show_current_user_attachments( $query ) {
    $user_id = get_current_user_id();
    if ( $user_id && !current_user_can('manage_other_users_attachments') && current_user_can('manage_own_attachments') ) {
        $query['author'] = $user_id;
    }
    return $query;
} 

I spent a while researching that so I thought I'd spare the trouble to other devs.

Laura

0

You can allow the Author role (or any custom role) in WordPress to delete Media Library items with the following snippet:

/**
 * Add Capability to Author Role to Delete Media Attachments
 */
add_action( 'admin_init', function() {
    $author = get_role( 'author' );

    if ( ! $author->has_cap( 'delete_posts' ) ) {
        $author->add_cap( 'delete_posts' );
    }
});

The capability to edit/delete attachment items in the WordPress Media Library is edit_posts and delete_post as shown in WP_Media_List_Table::_get_row_actions():


However, one challenge with WordPress sharing the edit_posts and delete_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:

/**
 * Remove Posts Admin Menu for Author Role
 */
add_action( 'admin_init', function () {
    $user = wp_get_current_user();

    if ( ! empty( $user->roles ) ) {
        if ( in_array( 'author', $user->roles ) ) {
            remove_menu_page( 'edit.php' );
            remove_menu_page( 'edit-comments.php' );
        }
    }
});

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 or delete_posts to a user role in WordPress.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.