Lorenzo Sfienti

How to disable comments into WordPress

One of the defining features of WordPress is its ability to facilitate user engagement through comments.

However, there may be instances where website owners or administrators prefer to disable this functionality.

Whether it’s due to spam concerns, a change in content strategy, or a desire for a more streamlined user experience, disabling comments can be achieved by adding a simple code snippet to the functions.php file of your WordPress theme.

In this article, we will guide you through the process of disabling the comment function in WordPress.

Step 1: Accessing the functions.php file

To begin, you need to access the functions.php file of your WordPress theme.

This file contains the code that controls the behavior and features of your website.

I suggest testing this change in a local environment.

Step 2: Adding the Code Snippet

Once you have accessed the functions.php file, you can add the code snippet that will disable the comment function on your website. Insert the following code at the end of the file

// Disable support for comments and trackbacks in post types
add_action('admin_init', function(){
    $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');
		}
	}
},PHP_INT_MAX);

// Close comments on the front-end
add_filter('comments_open', function(){
    return false;
}, PHP_INT_MAX);
// Close trackbacks on the front-end
add_filter('pings_open', function(){
    return false;
}, PHP_INT_MAX);

// Hide existing comments
add_filter('comments_array',function($comments){
    return array();
},PHP_INT_MAX,1);