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.

Is there any way to prevent/hide trash button for comments?

Note that I only want to prevent users from emptying the trash for comments. Can't seem to find a way. Is there any possible way to do that without plugin? But if I have to, I'll use a plugin.

This is what I've tried so far, but not getting it to work.

add_filter( 'post_row_actions', 'remove_row_actions', 10, 1 );
function remove_row_actions( $actions )
{
    if( get_post_type() === 'post' )
        unset( $actions['edit'] );
        unset( $actions['view'] );
        unset( $actions['trash'] );
        unset( $actions['inline hide-if-no-js'] );

    return $actions;
}
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

First, a note on you code: if you don't encapsulate the code of a conditional with curly brackets: if($something){ encapsulated_actions(); }
it will only execute the next line.

So, you should use:

if( 'post' != get_post_type() ) // if different of desired type, do nothing and return default
    return $actions; 

// do your thing and return modified var
unset ($actions['spam'], $actions['trash'] ); 
return $actions;

Or:

if( 'post' == get_post_type())
{
    unset ($actions['spam']); 
    unset ($actions['trash']); 
    /* The following is best use */
    // unset ($actions['spam'], $actions['trash'] ); 
}
// returns modified or unmodified var depending on previous check
return $actions;


We need to target four places to block/hide the Trashing of comments. Here, I'm checking for administrative role. To check for specific users, use get_current_user_id().

The function names indicate the ID of the original Question here in WordPress Answers, e.g., wpse_92313 is: Comments screen in backend, how to disable Quick Edit | Edit | History | Spam | for non admins

1) Quick edit action row

add_filter( 'comment_row_actions', 'comments_row_wpse_92313', 15, 2 );   
function comments_row_wpse_92313( $actions, $comment )
{
    /* Type of the parent post */
    // $parent_type = get_post_type( $comment->comment_post_ID ) );

    if( !current_user_can( 'delete_plugins' ) )
        unset($actions['trash'] );

    return $actions;
}

2) The Views row ( All | Pending | ... | Trash )

add_filter( 'views_edit-comments', 'wpse_30331_custom_view_count', 10, 1);   
function wpse_30331_custom_view_count( $views ) 
{
    if( !current_user_can( 'delete_plugins' ) )
        unset( $views['trash'] );

    return $views;
}

3) Bulk actions

I've just discovered the filter hook 'bulk_actions-' . $this->screen->id

add_filter( 'bulk_actions-edit-comments', 'remove_bulk_trash_wpse_92534' );   
function remove_bulk_trash_wpse_92534( $actions )
{
    if( !current_user_can( 'delete_plugins' ) )
        unset( $actions['trash'] );

    return $actions;
}

4) Blocking direct access to trash screen

Redirects example.com/wp-admin/edit-comments.php?comment_status=trash to the main Comments screen.

add_action( 'admin_head-edit-comments.php', 'wpse_74488_block_trash_access' );   
function wpse_74488_block_trash_access()
{
    // Don't run the function for Admins
    if( current_user_can( 'delete_plugins' ) )
        return;

    if( isset( $_GET['comment_status'] )  && 'trash' == $_GET['comment_status'] ) 
    {
        wp_redirect( admin_url( 'edit-comments.php' ) ); 
        exit;
    }
}
share|improve this answer
    
Thanks for the fast detailed reply! Your code works perfectly except that i wanna show the trash , but then in the trash i wanna hide the "delete peremenatly" @brasofilo –  M3o Mar 27 '13 at 14:08
    
@MeiasSafa, it was a nice investigation ;) - To target the 'delete' action, use $actions['delete'] in the functions comments_row_wpse_92313 and remove_bulk_trash_wpse_92534. –  brasofilo Mar 27 '13 at 14:28
    
Thank you, genuis :D! –  M3o Mar 27 '13 at 15:04
    
@brasfilo - if you look at your nr 2 suggestion, is there anyway to rename the field in the same function ? or to hide the "Empty Trash" button. –  M3o Apr 2 '13 at 8:50
add comment

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.