WordPress Function to Remove “Comments” link from admin bar if comment status is closed

Remove “Comments” link from admin bar if comment status is closed

You can set the default comment status to ‘closed’, but the comments link will stay. Simply drop the following into your functions.php file to get rid of it based on the condition. Offers 2 different approaches.

/**
 * Disable 'Comments' link if default status is _closed_
 */
function remove_comments() 
{
    $default_comment_status = get_option( 'default_comment_status' );

    if ( $default_comment_status == 'closed' ) 
    {
        remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 50 );

        // optional solution using the WP_Admin_Bar class from /wp-includes/class-wp-admin-bar.php
        # global $wp_admin_bar;
        # $wp_admin_bar->remove_menu( 'comments' );
    }
    else 
    {
        return;
    }
}

You may also like...

Leave a Reply

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