Disable comments for attachment pages in WordPress

WordPress allows you to write comments on media attachment pages by default. Since attachment pages contain only the attachment, it’s unlikely that people will write comments on them. The snippet below disables comments on attachment pages:

add_filter('comments_open', function($is_open, $post_id) {
	return $is_open && get_post($post_id)->post_type !== 'attachment';
}, 10, 2);
If $is_open is already false, this doesn’t fetch the post.

Leave a Reply

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