Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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;
    }
}
share|improve this answer
1  
Works elegantly! I'm impressed, great solution, upvoted and accepted! –  Amanda Duke Dec 1 '12 at 16:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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