DEV Community

Cover image for Disable Comments in WordPress Using Functions.php Without Plugin
Roy jemee
Roy jemee

Posted on • Updated on

Disable Comments in WordPress Using Functions.php Without Plugin

Comments on a WordPress website can be a great way to engage with your audience, gather feedback, and foster a sense of community.

However, there are situations where you might want to disable comments on your WordPress site. Perhaps you're running a static website, an e-commerce store, or a portfolio site where comments are unnecessary or unwanted.

In such cases, it's essential to know how to completely disable comments in WordPress. This completely means, my solution will help you to remove comments menu from admin, Comments Dashboard Widget, Comment form from any post type, Comments on the attachments page.

In this blog post, we'll guide you through the process of disabling comments in WordPress using the functions.php file, but first, let's cover why you might want to do this.

Why Disable Comments in WordPress?

  1. Spam Control: Comments can attract spammy content and require constant monitoring or filtering. Disabling comments can eliminate this hassle.

  2. Content Focus: Some websites aim to deliver content without any distractions, making comments irrelevant.

  3. Reduced Maintenance: If you're not actively moderating or responding to comments, disabling them can reduce the overall maintenance workload.

  4. Improved Page Load Speed: Fewer comments mean faster page load times, enhancing user experience.

Before we dive into the process, it's essential to take some precautions.

1. Backup Your Website:
Before making any significant changes to your website, it's crucial to back up your data. This ensures that you can restore your website to its previous state if something goes wrong during the process.

2. Create a Child Theme:
To maintain the flexibility to update your theme in the future, it's recommended to create a child theme. This way, your modifications won't be overwritten when you update your main theme.

Now, let's explore how to disable comments in WordPress.

Disabling Comments Using functions.php:

WordPress allows you to disable comments globally by adding a simple code snippet to your theme's functions.php file. Here's how you can do it:

31 Sec video clip to Disable WordPress Comments.

  1. Access your FTP or Cpanel File manager

  2. Navigate to your Active Theme directory > Search for Function.php file

  3. Add the following code snippet at the end of the file:

// Disable comments on posts
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('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 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');

Enter fullscreen mode Exit fullscreen mode
  1. Save the changes.

That's it! Comments are now disabled on your WordPress site. You can also explore Comments on WordPress docs for some general info to control Comments from the Dashboard.

An Easier Alternative: WP Adminify Plugin:

If dealing with code seems daunting, there's a user-friendly alternative: the WP Adminify plugin. It offers a "Disable Comments" module that lets you disable comments on your site with a single click, making it a hassle-free solution for those who prefer a more straightforward approach.

Disable Comments WordPress by WP Adminify

Take a look at all of the options panel from Disable Comment module by WP Adminify and control yourself.

Conclusion:

Disabling comments in WordPress can be a smart move for specific websites, whether it's for security, content focus, or improved page performance.

Remember always to back up your website and consider creating a child theme before making any changes. If you prefer an easier way to disable comments, the WP Adminify plugin offers a convenient solution.

Furthermore, WP Adminify isn't just about disabling comments. It provides numerous features to personalize your WordPress dashboard and enhance your overall WordPress experience. Give it a try and see how it can improve your site management and customization.

We hope this guide helps you effectively disable comments on your WordPress website and tailor your site to your specific needs. Enjoy a cleaner, more focused, and faster website without the hassle of unwanted comments.

Top comments (0)