Technical
Web Development, WordPress

Disable Comment Support for WordPress Posts and Pages

Craig Marolf
Web Developer and Marketing Strategist
Published on October 20, 2024
By Craig Marolf in Technical

Share

WordPress is often used to build websites that don’t have a blog or have blogs with no use for commenting. While you can disable the comments from within the WordPress admin, you may want to disable the comment functionality entirely.

To completely disable comments in WordPress via the functions.php file, you can add custom code that removes comment functionality across your entire site. This is helpful if you want to ensure that comments are disabled for all posts, pages, and custom post types.

1. Disable Comment Support for Posts and Pages

Add the following code to your theme’s functions.php file. This will disable comments for all post types (like posts, pages, etc.).

// 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');

2. Close Comments on the Frontend

The following code will ensure that comments cannot be displayed on the frontend, even if comments exist for older posts.

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

3. Hide Existing Comments

If there are existing comments, you can hide them from being displayed on the site by adding this filter to prevent any comment-related content from being loaded.

// Hide existing comments
function disable_comments_hide_existing($comments) {
    return [];
}
add_filter('comments_array', 'disable_comments_hide_existing', 10, 2);

4. Remove Comments from the Admin Menu

The following code will remove the comments section from the WordPress admin menu.

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

5. Redirect Any User Trying to Access Comments Page

If users try to access the comments page via direct URL, you can redirect them to the dashboard.

// 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');

6. Remove Comments Metabox from Dashboard

You can also remove the “Recent Comments” metabox from the WordPress dashboard.

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

7. Remove Comments Links from Admin Bar

Finally, to fully clean up the admin interface, you can remove the comments link from the WordPress admin bar.

// 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');

All of the above code snippets together

// 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 frontend
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) {
    return [];
}
add_filter('comments_array', 'disable_comments_hide_existing', 10, 2);

// Remove comments page in the admin 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');