PHPwordpressbeginner

Disable WordPress Comments Globally

Completely disable comments across your entire WordPress site

#wordpress#comments#disable#spam#optimization
Share this snippet:

Code

php
1// Disable support for comments and trackbacks in post types
2function disable_comments_post_types_support() {
3 $post_types = get_post_types();
4 foreach ($post_types as $post_type) {
5 if (post_type_supports($post_type, 'comments')) {
6 remove_post_type_support($post_type, 'comments');
7 remove_post_type_support($post_type, 'trackbacks');
8 }
9 }
10}
11add_action('admin_init', 'disable_comments_post_types_support');
12
13// Close comments on the front-end
14function disable_comments_status() {
15 return false;
16}
17add_filter('comments_open', 'disable_comments_status', 20, 2);
18add_filter('pings_open', 'disable_comments_status', 20, 2);
19
20// Hide existing comments
21function disable_comments_hide_existing_comments($comments) {
22 $comments = array();
23 return $comments;
24}
25add_filter('comments_array', 'disable_comments_hide_existing_comments', 10, 2);
26
27// Remove comments page in menu
28function disable_comments_admin_menu() {
29 remove_menu_page('edit-comments.php');
30}
31add_action('admin_menu', 'disable_comments_admin_menu');
32
33// Redirect any user trying to access comments page
34function disable_comments_admin_menu_redirect() {
35 global $pagenow;
36 if ($pagenow === 'edit-comments.php') {
37 wp_redirect(admin_url());
38 exit;
39 }
40}
41add_action('admin_init', 'disable_comments_admin_menu_redirect');
42
43// Remove comments metabox from dashboard
44function disable_comments_dashboard() {
45 remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
46}
47add_action('admin_init', 'disable_comments_dashboard');
48
49// Remove comments links from admin bar
50function disable_comments_admin_bar() {
51 if (is_admin_bar_showing()) {
52 remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
53 }
54}
55add_action('init', 'disable_comments_admin_bar');

Disable WordPress Comments Globally

This snippet completely disables comments across your WordPress site, removes comment-related admin menu items, and hides comment forms from the frontend.

// Disable support for comments and trackbacks in post types
function 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('admin_init', 'disable_comments_post_types_support');

// Close comments on the front-end
function disable_comments_status() {
    return false;
}
add_filter('comments_open', 'disable_comments_status', 20, 2);
add_filter('pings_open', 'disable_comments_status', 20, 2);

// Hide existing comments
function disable_comments_hide_existing_comments($comments) {
    $comments = array();
    return $comments;
}
add_filter('comments_array', 'disable_comments_hide_existing_comments', 10, 2);

// Remove comments page in menu
function disable_comments_admin_menu() {
    remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'disable_comments_admin_menu');

// Redirect any user trying to access comments page
function disable_comments_admin_menu_redirect() {
    global $pagenow;
    if ($pagenow === 'edit-comments.php') {
        wp_redirect(admin_url());
        exit;
    }
}
add_action('admin_init', 'disable_comments_admin_menu_redirect');

// Remove comments metabox from dashboard
function disable_comments_dashboard() {
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'disable_comments_dashboard');

// Remove comments links from admin bar
function disable_comments_admin_bar() {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
}
add_action('init', 'disable_comments_admin_bar');

Features

  • Complete Removal: Disables comments site-wide
  • Admin Cleanup: Removes comment-related menu items
  • Security: Prevents access to comment admin pages
  • Performance: Reduces database queries
  • Clean UI: Hides all comment-related elements

Alternative: Disable for Specific Post Types

// Disable comments only for pages and custom post types
function disable_comments_specific_post_types() {
    $post_types = array('page', 'custom_post_type');
    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('admin_init', 'disable_comments_specific_post_types');

Related Snippets