Completely Disable Comments

Completely disable comments across the site including admin and front-end.

PHP
/**
 * Snippet Name:     Completely disable comments
 * Snippet Author:   coding-bunny.com
 * Description:      Completely disable comments across the site including admin and front-end.
 */

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action('admin_init', 'cb_disable_comments_admin_init');

function cb_disable_comments_admin_init() {
    // Redirect any user trying to access the comments page
    global $pagenow;

    if ($pagenow === 'edit-comments.php') {
        wp_safe_redirect(admin_url());
        exit;
    }

    // Remove the recent comments metabox from the dashboard
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

    // Disable support for comments and trackbacks in post types
    foreach (get_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');
        }
    }
}

// Close comments on the front-end
add_filter('comments_open', 'cb_close_comments', 20, 2);
add_filter('pings_open', 'cb_close_comments', 20, 2);

function cb_close_comments($open, $post_id) {
    return false;
}

// Hide existing comments
add_filter('comments_array', 'cb_hide_existing_comments', 10, 2);

function cb_hide_existing_comments($comments, $post_id) {
    return [];
}

// Remove the comments page from the admin menu
add_action('admin_menu', 'cb_remove_comments_menu');

function cb_remove_comments_menu() {
    remove_menu_page('edit-comments.php');
}

// Remove comment links from the admin bar
add_action('admin_bar_menu', 'cb_remove_admin_bar_comments', 0);

function cb_remove_admin_bar_comments($wp_admin_bar) {
    $wp_admin_bar->remove_node('comments');
}

How To Implement This Solution?

Leave a Reply