NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

Disabling Comments on your Blog
Easy
Web Hosting Canada

Last modified : Nov 18 2021

Estimated reading time : 0 minutes, 57 seconds - 115 words

With this tutorial, we will show you a function that allows you to disabled or tweak comments left on your website by users. The function users the comments_open filter.

Here’s the function for the condition: $post = get_post( $post_id ); and if $post->post_type correspond à post (which is the default post type for articles/posts), then the $open variable must return false.

Disabling comments on your blog

add_filter('comments_open', 'lenomdemafonction', 10, 2);
function lenomdemafonction( $open, $post_id ) {
$post = get_post( $post_id );
if ('post' == $post->post_type)
$open = false;
return $open;
}

We could also disable comments on your pages only; simply replace ‘post’ by ‘page’. For post types, simply had the post title instead of ‘post’.

Don’t forget that under Discussion settings, the option that allows users to leaves comments must be left unchecked.

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>