Restrict access to the Trash folder in Posts

I want to restrict users from deleting posts permanently. How do I restrict access to the Trash folder in Posts? This is the only page were the users can delete their posts permanently.

The only solutions that I have so far is:

Read More

1) Adding this to stylesheet:

li.trash {
    display: none;
}

But both me and you know that this is a dummy fix and more importantly, it just hides the link that goes to the Trash folder for all users.

2) Remove the capability delete_posts for none-editors. The problem with this is that the users will not be able to move their posts to the trash folder (which is a feature that is essential for my platform).

Do you know of a better solution?

Related posts

Leave a Reply

1 comment

  1. With the filter we prevent the Trash link from being printed.
    And with the action, redirect back to the post listing page if the user tries to access the URL directly (/wp-admin/edit.php?post_status=trash&post_type=post).

    add_filter( 'views_edit-post', 'wpse_74488_remove_trash_link' );
    add_action( 'admin_head-edit.php', 'wpse_74488_block_trash_access' );
    
    function wpse_74488_remove_trash_link( $views ) 
    {
        if( !current_user_can( 'delete_plugins' ) )
            unset( $views['trash'] );
    
        return $views;
    }
    
    function wpse_74488_block_trash_access()
    {
        global $current_screen;
    
        if( 
            'post' != $current_screen->post_type 
            || 'trash' != $_GET['post_status'] 
        )
            return;
    
        if( !current_user_can( 'delete_plugins' ) )
        {
            wp_redirect( admin_url() . 'edit.php' ); 
            exit;
        }
    }