Hide Comments in the Admin From Users Who Cannot Moderate
Removes the Comments menu, admin bar bubble and screen for users without moderation rights, such as authors and contributors.
Opens WPIDE on
Code
add_action( 'admin_menu', function () {
if ( ! current_user_can( 'moderate_comments' ) ) {
remove_menu_page( 'edit-comments.php' );
}
}, 999 );
add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {
if ( ! current_user_can( 'moderate_comments' ) ) {
$wp_admin_bar->remove_node( 'comments' );
}
}, 999 );
add_action( 'load-edit-comments.php', function () {
if ( ! current_user_can( 'moderate_comments' ) ) {
wp_safe_redirect( admin_url() );
exit;
}
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.