How to Disable Comments Programmatically in WordPress

How to Disable Comments Programmatically in WordPress

Sometimes you may need to programmatically disable the comment form. This can be done using the “comments_open” hook:

add_filter('comments_open', function($open, $post_id)
{
	return false;
}, 10, 2);

This hook accepts two parameters:

$open — whether the current post is open for comments
$post_id — the ID of the current post

It’s a very useful hook that allows you to dynamically enable or disable comment visibility.

In combination with it, you can use the “comment_form_comments_closed” hook, which is triggered when commenting is disabled:

add_action('comment_form_comments_closed', function()
{
	echo '[TEST]';
});
Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

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