robert0
robert0

Reputation: 445

How To Disable (or Remove) “All Comments, Published, and Trash” in WP Dashboard for non admins

So I have found the way to remove all of that in Posts (for non admins) with the following line:

/**
 * Remove the 'all', 'publish', 'future', 'sticky', 'draft', 'pending', 'trash' 
 * views for non-admins
 */
add_filter( 'views_edit-post', function( $views )
{
    if( current_user_can( 'manage_options' ) )
        return $views;

    $remove_views = [ 'all','publish','future','sticky','draft','pending','trash' ];

    foreach( (array) $remove_views as $view )
    {
        if( isset( $views[$view] ) )
            unset( $views[$view] );
    }
    return $views;
} );

Now I want to remove all of those in Comments as well.

I can't find the answer.

Any help would be appreciated.

Upvotes: 0

Views: 118

Answers (1)

Lucius
Lucius

Reputation: 1333

As said in the comments, just add another add_filter with "views_edit-comments".

To always show only own comments to a non-admin user, use the follow code:

add_action( 'current_screen', 'wp_66446729_filter_comments', 10, 2 );

function wp_66446729_filter_comments( $screen )
{
    if ( current_user_can('administrator') )
        return;

    add_action( 'pre_get_comments', 'wp_66446729_list_own_comments_only', 10, 1 );
}

function wp_66446729_list_own_comments_only( $clauses )
{
    $user_id = get_current_user_id();
    if ($user_id) {
      $clauses->query_vars['user_id'] = $user_id;
    }
}

Upvotes: 1

Related Questions