1

I would like to hide "Comments" menu in Dashboard until the site doesn't have any comments. I tried to do this with this below code, but doesn't works:

function remove_mainmenu_pages() {
        global $user_ID;
        if ( 1 > wp_count_comments( get_the_ID() )->all ) {
                remove_menu_page( 'edit-comments.php' );
    }
}
add_action( 'admin_menu', 'remove_mainmenu_pages' );

Thanks!

2
  • Other than the get_the_ID() shouldn't be used there.. the code seems good. Have you tried echo 'something'; in the function, to see if it's being called? – Sally CJ Nov 13 '19 at 17:26
  • @SallyCJ yes, it seems the echo works. I modified the code in my question. Now, the Comments menu disappear when the comment number is zero, except if I have spam or trash comments. How can I add these types to count as well? – Feriman Nov 13 '19 at 18:53
1

Okay, the solution is this below to hide Comments menu if the site doesn't have any comments (approved, pending, trash or spam).

function remove_commentsmenu() {
        global $user_ID;
        if ( 1 > wp_count_comments( get_the_ID() )->total_comments && 1 > wp_count_comments( get_the_ID() )->trash ) {
                remove_menu_page( 'edit-comments.php' );
    }
}
add_action( 'admin_menu', 'remove_commentsmenu' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.