How to Hide Pending Comments from Contributors in WordPress

This is actually a huge problem.

You can’t possibly restrict the contributors on your WordPress blog from accessing pending, approved, trash, spam and every other comment within the admin dashboard.

There’s no way you can do so by installing a plugin like Capabilities. There is no option to set user permissions that can hide comments in the backend from contributors.

And I wanted a solution so I went online, searched a few forums and other WordPress projects. To my surprise, 99% of the methods to solve this problem are baloney.

One method worked, but it also made the comments in the front end invisible to the logged-in users. Not an ideal solution.

Finally, one piece of PHP code worked!

Now my contributors won’t see any comments in the WordPress admin, the backend dashboard.

comments successfully hidden from contributors WordPress

But they can still access the comments in the front-end, on the blog post itself, This way, they can respond to the comments they receive on their blog posts easily.

They can’t see any comments inside the WordPress dashboard, whether approved or not as well as those that are in the trash. That’s called complete privacy.

Below the code, you need to copy and paste inside the functions.php file of the WordPress website where you wish to hide comments in the backend from your contributors.

Access your server via FTP using FileZilla, you will find the functions.php file inside the theme sub-folder under the wp-content main folder.  Alternatively, you can use the wp-file editor plugin.

Hide comments from contributors:

function filter_comments_by_contributor( $all_comments ) {
// get the current logged in user
$current_user = wp_get_current_user();

if ( 0 == $current_user->ID ) {
// Not logged in.
return $all_comments;
} else {
// Logged in.

// check if the logged-in user is a contributor
if ( in_array( 'contributor', (array) $current_user->roles ) ) {

// check if the user is on wp-admin backend,
$screen = get_current_screen();
if ( ! empty( $screen ) && 'edit-comments' == $screen->id ) {

// get all posts by that contributor
$args = array(
'author' => $current_user->ID,
'posts_per_page' => - 1,
'fields' => 'ids'
);
$contributor_posts = get_posts( $args );

// unset the comments given on posts other than his/her.
foreach ( $all_comments as $key => $value ) {
if ( ! in_array( $value->comment_post_ID, $contributor_posts ) ) {
unset( $all_comments[ $key ] );
}
}
}

return $all_comments;
} else {
return $all_comments;
}
}
}

if( is_user_logged_in() && !current_user_can('manage_options')) {
add_filter( 'the_comments', 'filter_comment_by_contributor' );
}

If you are new to WordPress or are non-techy, the best way to do this is by installing the Code Snippets plugin. You can simply copy/paste this code and save it. Code Snippets will add the code to your functions.php file for you.

code snippets hide comments php code

If you mistakenly did something wrong such as adding a hyphen in the code, the plugin won’t allow you to save it. So, it’s a nice way to make sure your site doesn’t break and you can safely add this code.

That’s it. Use the code above and you’ll be able to hide all the comments in your WordPress baked from your contributors. They would still be visible on the front-end, on the posts if you approve the comments.

Let me know if you have any questions.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *