isotropic-2022
Share
Blog
Search
Menu

How To Disable Comments On A WordPress Website (No Plugin)

By James LePage
 on January 4, 2021
Last modified on January 6th, 2022

How To Disable Comments On A WordPress Website (No Plugin)

By James LePage
 on January 4, 2021
Last modified on January 6th, 2022

In this article, we're going to show you how to disable comments on a WordPress website without the need for a plugin.

The WordPress content management system was initially created for bloggers, and the commenting system was mandatory for that application. However, as it grew, many people began using it for other purposes like corporate websites, which don't need comments. In fact, if you keep comments enabled, you're opening yourself up to spam on these type of websites.

Because of that, it's important to understand how to disable comments on a WordPress website. There are many plugins that help you do just that, but if you are looking for a method that doesn't use a plugin (adding more bulk to your site) there's an easy code snippet that you can use. In this article, we're going to show you that code snippet, and how to implement it into your WordPress website, closing your comments, and eliminating any potential for spam.

Code Snippet To Disable WordPress Comments

<?php

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

    // Remove comments metabox from 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', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);

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

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

// Remove comments links from admin bar
add_action('init', function () {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
});Code language: HTML, XML (xml)

This code is sourced from this GitHub, and we highly recommend checking it out because there is some nice discussion, and additional snippets that you can use to disable other aspects of the commenting system on WordPress.

How To Use It

All you need to do is copy and paste the source code directly into a code snippet, or your functions.php file. For script management, we highly recommend the Advanced Scripts tool. Simply copy and paste the code into a new advanced scripts PHP snippet, and hook into the init.

If you use Advanced Scripts 2.0, you can download this file and import the script directly into your website. All you need to do is toggle it on, and you're good to go. No more comments on your WordPress website.

(download currently unavailable)

Another Method To Disable Comments (No Code)

You can also disable comments by going to the settings tab, discussions tab, disabling commenting there, and then bulk editing all your posts, and turning off commenting and pingbacks/trackbacks for each of them. More info on this method can be found here.

Why not a plugin?

There's nothing inherently wrong with using a plugin to disable WordPress comments, except for the fact that it is an incredibly simple thing to do with a code snippet, and any additional plugin adds bulk to a WordPress website. We prefer avoiding the use of plugins if at all possible, and this solution allows us to do just that. However, if you really want to use a plugin to disable comments on WordPress, this free offering from WPDeveloper works well:

Conclusion

We have used this method to disable all comments on a WordPress website for the past couple of years. This is a necessity if you aren't utilizing the blog functionality of the WordPress CMS, as it limits spam, and makes your website more secure.

Subscribe & Share
If you liked this content, subscribe for our monthly roundup of WordPress news, website inspiration, exclusive deals and interesting articles.
Unsubscribe at any time. We do not spam and will never sell or share your email.
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
John Anderson
2 years ago

Thanks for this handy bit of code. I've started exporting my most popular code snippets from Advanced Scripts for easy uploads to future builds. I'll add this to the collection.

Article By
James LePage
Contributors/Editors
notloggedin
James LePage is the founder of Isotropic, a WordPress education company and digital agency. He is also the founder of CodeWP.ai, a venture backed startup bringing AI to WordPress creators.
We're looking for new authors. Explore Isotropic Jobs.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram