How to Completely Disable Comments in WordPress

Comments are an essential feature of many websites, providing a platform for discussions and engagement. However, there are instances when you might want to eliminate comments site-wide. This tutorial will guide you through the process of completely disabling comments in WordPress, whether you’re building a non-blog site or prefer alternative methods of managing discussions.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Open the Theme Functions File

Within your active child-theme folder, locate the functions.php file. This file contains functions and code that influence your theme’s behavior. We’ll be adding our code snippets here.

Redirect Users Trying to Access Comments Page

To disable comments, you need to restrict access to the comments page in the WordPress admin panel. Copy and paste the following code snippet at the end of your functions.php file:

add_action('admin_init', function () {
    // Redirect any user trying to access comments page
    global $pagenow;

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

This code snippet ensures that users attempting to access the comments page are redirected to the admin dashboard instead.

Explanation:
The admin_init action hook triggers the function provided. Inside the function, we check if the current page is edit-comments.php and use wp_safe_redirect to send users back to the admin dashboard.

Remove Comments Metabox from Dashboard

In this step, we’ll clean up the dashboard by removing the comments metabox. Add the following code snippet to your functions.php file:

// Remove comments metabox from dashboard
add_action('wp_dashboard_setup', function () {
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
});
Code language: PHP (php)

This code snippet ensures that the comments metabox, which displays recent comments, is removed from the WordPress dashboard.

Explanation:
The wp_dashboard_setup action hook triggers the function provided. Inside the function, we use remove_meta_box to eliminate the comments metabox from the dashboard.

Disable Support for Comments and Trackbacks in Post Types

Next, we’ll disable the ability to add comments and trackbacks to all post types. Paste the following code snippet into your functions.php file:

// Disable support for comments and trackbacks in post types
add_action('init', function () {
    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');
        }
    }
});
Code language: PHP (php)

This code snippet ensures that comments and trackbacks are disabled across all post types.

Explanation:
The init action hook triggers the function provided. Inside the function, we iterate through all registered post types and use remove_post_type_support to disable the support for comments and trackbacks.

Close Comments on the Front-End

Now we’ll close comments and trackbacks on the front-end of your website. Paste the following code snippet into your functions.php file:

// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
Code language: PHP (php)

This code snippet ensures that users can’t leave comments or trackbacks on the front-end of your website.

Explanation:
The comments_open and pings_open filters are used to return false, effectively closing comments and trackbacks on the front-end.

Hide Existing Comments

If you’ve already accumulated comments on your posts, you might want to hide them. Paste the following code snippet into your functions.php file:

// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
Code language: PHP (php)

This code snippet ensures that existing comments are hidden from being displayed on your posts.

Explanation:
The comments_array filter is used to return an empty array, effectively hiding existing comments from appearing.

Remove Comments Page in Menu

To complete the process, let’s remove the comments page from the WordPress admin menu. Paste the following code snippet into your functions.php file:

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

This code snippet ensures that the comments page is removed from the WordPress admin menu.

Explanation:
The admin_menu action hook triggers the function provided. Inside the function, we use remove_menu_page to eliminate the comments page from the admin menu.

Remove Comments Links from Admin Bar

Lastly, we’ll remove any links related to comments from the WordPress admin bar. Paste the following code snippet into your functions.php file:

// Remove comments links from admin bar
add_action('admin_bar_menu', function () {
    remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}, 0);
Code language: PHP (php)

This code snippet ensures that comments-related links are removed from the WordPress admin bar.

Explanation:
The admin_bar_menu action hook triggers the function provided. Inside the function, we use remove_action to eliminate the comments menu item from the admin bar.

That’s it! You’ve successfully disabled comments across your entire WordPress website. By following the steps outlined in this tutorial, you’ve gained control over the interactions on your platform. This method is particularly useful when you want to focus on other forms of engagement or simply want a comments-free experience for your users.

Leave your feedback and help us improve šŸ¶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ā˜•ļø or donating via PayPal šŸ’°.

More free knowledge, because why not?

Your thoughts matter, leave a reply šŸ’¬

Your email address will not be published. Required fields are marked *