Disable WP comments but leave Woocommerce product review opened

Nowadays, the main purpose of WordPress is not merely for blogging anymore. In fact, blogging is not trendy since the rise of social media emerged. People use WordPress mainly for business CMS (content management system) or E-commerce platform. Hence the comment feature is not that relevant anymore.

Of course You can disable the comment features in the admin backend, but if you running a woocommerce powered e-commerce website, then you will need to leave it enabled so that the product’s review feature works. What about if you want the product review feature opened while post comment feature is off?

Here is how you can do it:

add_filter( 'comments_open', 'allow_comments_on_product_only', 15 , 2 );
function allow_comments_on_product_only($open, $post_id = null){
	if(!$post_id) return false;
	$post = get_post( $post_id );
	
	if( !$post->post_type == 'product' ) {
            return false; //only disable the comment when current page is not product
        }
	return $open;
}

add_filter( "comments_array", "hide_comments_array_defaults", 10, 2 );
function hide_comments_array_defaults($comments, $post_id) { 

    if(!$post_id) return [];
	$post = get_post( $post_id );
	
	if( !$post->post_type == 'product' ) {
            return []; //hide the previous comments if it is not from product page
         }
	return $comments;
}

You can use the snippet above for other post type as well. The post_type conditional check is where you decide which post type you wish the comment feature is open.

That’s all. Happy coding.

Leave A Comment