How to Disable All Comments on Your WordPress Site

Home » Snippets » How to Disable All Comments on Your WordPress Site
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Comments can be a valuable feature on a WordPress site, as they allow visitors to engage with your content and offer feedback. However, in some cases, website owners may want to disable comments entirely. This could be because they are worried about spam or inappropriate comments, or simply because they don’t want to moderate comments at all. In this article, we’ll walk you through the steps to disable comments on your WordPress site, giving you complete control over your site’s content.

					function wpturbo_disable_comments_post_types_support() {
    $post_types = get_post_types();
    foreach ( $post_types as $post_type ) {
        if ( post_type_supports( $post_type, 'comments' ) ) {
            remove_post_type_support( $post_type, 'comments' );
            remove_post_type_support( $post_type, 'trackbacks' );
        }
    }
}
add_action( 'init', 'wpturbo_disable_comments_post_types_support' );

function wpturbo_disable_comments_status() {
    return false;
}
add_filter( 'comments_open', 'wpturbo_disable_comments_status', 20, 2 );
add_filter( 'pings_open', 'wpturbo_disable_comments_status', 20, 2 );

function wpturbo_disable_comments_admin_menu() {
    remove_menu_page( 'edit-comments.php' );
}
add_action( 'admin_menu', 'wpturbo_disable_comments_admin_menu' );

function wpturbo_disable_comments_admin_menu_redirect() {
    global $pagenow;
    if ( $pagenow === 'edit-comments.php' ) {
        wp_redirect( admin_url() );
        exit;
    }
}
add_action( 'admin_init', 'wpturbo_disable_comments_admin_menu_redirect' );

function wpturbo_disable_comments_dashboard() {
    remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
add_action( 'admin_init', 'wpturbo_disable_comments_dashboard' );

function wpturbo_disable_comments_admin_bar() {
    if ( is_admin_bar_showing() ) {
        remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
    }
}
add_action( 'init', 'wpturbo_disable_comments_admin_bar' );
				

In this article, we will learn how to disable all comments on your WordPress website using custom code. If you’re building a website that doesn’t need user comments, you can use this code snippet to easily remove them from your site.

The code snippet provided includes several functions, so we’ll cover each one separately:

The first function, named wpturbo_disable_comments_post_types_support, disables post types support for comments and trackbacks. It loops through all post types on your site (such as posts, pages, and custom post types), checks if comments or trackbacks are supported for that post type, and removes that support if it is present. Removing support for comments and trackbacks ensures that your site won’t show comment-related fields in the back-end or front-end.

$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
    if ( post_type_supports( $post_type, 'comments' ) ) {
        remove_post_type_support( $post_type, 'comments' );
        remove_post_type_support( $post_type, 'trackbacks' );
    }
}

The next two functions, wpturbo_disable_comments_status and add_filter, filter the comments_open and pings_open hooks respectively, returning a false value to disable comments and trackbacks on all posts and pages.

function wpturbo_disable_comments_status() {
    return false;
}
add_filter( 'comments_open', 'wpturbo_disable_comments_status', 20, 2 );
add_filter( 'pings_open', 'wpturbo_disable_comments_status', 20, 2 );

The third function, wpturbo_disable_comments_admin_menu, removes the "Comments" menu option from the admin menu. This step is only for administrative purposes and won’t affect your website’s functionality.

function wpturbo_disable_comments_admin_menu() {
    remove_menu_page( 'edit-comments.php' );
}
add_action( 'admin_menu', 'wpturbo_disable_comments_admin_menu' );

The fourth function, wpturbo_disable_comments_admin_menu_redirect, redirects users to the dashboard when they try to access the "Comments" page directly through the URL. This step is also only for administrative purposes and won’t affect your website’s functionality.

function wpturbo_disable_comments_admin_menu_redirect() {
    global $pagenow;
    if ( $pagenow === 'edit-comments.php' ) {
        wp_redirect( admin_url() );
        exit;
    }
}
add_action( 'admin_init', 'wpturbo_disable_comments_admin_menu_redirect' );

The fifth function, wpturbo_disable_comments_dashboard, removes the "Recent Comments" meta box from the dashboard. This step is only for administrative purposes and won’t affect your website’s functionality.

function wpturbo_disable_comments_dashboard() {
    remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
add_action( 'admin_init', 'wpturbo_disable_comments_dashboard' );

Finally, the sixth function, wpturbo_disable_comments_admin_bar, removes the "Comments" link from the admin bar. This last step completes the process of disabling comments on your site.

function wpturbo_disable_comments_admin_bar() {
    if ( is_admin_bar_showing() ) {
        remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
    }
}
add_action( 'init', 'wpturbo_disable_comments_admin_bar' );

That’s it! Now you’ve successfully disabled all comments and related settings on your WordPress website.

Register an account to save your snippets or go Pro to get more features.