Completely Disable Comments in WordPress

01/30/2024
Back to Gists
<?php
/**
 * Customizes the WordPress admin and front-end to disable comments and trackbacks.
 *
 * Gist Keywords: wordpress, comments, trackbacks, functions
 *
 * This code hooks into various WordPress actions and filters to:
 * 1. Redirect users away from the comments page in the admin area.
 * 2. Remove the 'Recent Comments' metabox from the WordPress dashboard.
 * 3. Disable support for comments and trackbacks in all post types.
 * 4. Close comments and pingbacks/trackbacks on the front-end.
 * 5. Hide existing comments from being displayed on the front-end.
 * 6. Remove the comments page link and the comments link from the WordPress admin menu and admin bar.
 *
 * @author Knol Aust
 */

// Redirect from comments page and remove comments metabox in admin dashboard.
add_action('admin_init', 'knolaust_customize_admin_comments');
function knolaust_customize_admin_comments() {
    global $pagenow;
    if ($pagenow === 'edit-comments.php') {
        wp_safe_redirect(admin_url());
        exit;
    }
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
    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 and trackbacks on the front-end.
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
add_filter('comments_array', '__return_empty_array', 10, 2);

// Remove comments page and link from the admin menu and admin bar.
add_action('admin_menu', 'knolaust_remove_comments_admin_menu');
function knolaust_remove_comments_admin_menu() {
    remove_menu_page('edit-comments.php');
}
add_action('admin_bar_menu', 'knolaust_remove_comments_admin_bar', 0);
function knolaust_remove_comments_admin_bar() {
    remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}