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;
}
}