In this tutorial I will share a simple snippet which allows you to disable / hide comments on Blog Posts of selected categories.
The snippet is quite simple and does two things
1) It first checks if the post belongs to disabled categories. The check is done after the post object is completely created.
2) In case the post belongs to disabled category, the code uses comments_open() filter to close the comments on the particular post.
Here is the snippet.
Simply paste it to your functions.php file and you will be good to go
add_action( 'the_post', 'st_check_for_closed' );
function st_check_for_closed()
{
global $post;
$my_post_cat = wp_get_post_categories($post->ID);
$disabled_cat = array( "1", "3"); // this is he array of disabled categories. Feel free to edit this line as per your needs.
$my_result = array_intersect($my_post_cat,$disabled_cat);
if (empty ( $my_result ) )
{
return;
}
else {
add_filter( 'comments_open', 'st_close_comments_on_category', 10, 2 );
add_action('wp_enqueue_scripts', 'st_deregister_reply_js');
}
}
function st_deregister_reply_js()
{
wp_deregister_script( 'comment-reply' );
}
function st_close_comments_on_category ($open, $post_id)
{
$open = false;
}
Here is an Explanation of the logic behind the script.
1) Hook into the_post action:
According to the codex, the_post allows us to modify the post object after it has been setup. We are hooking a custom function st_check_for_closed which checks if the post belongs to a disabled category.
2) Get the post categories:
We will use the function wp_get_post_categories to get the categories to which the post belongs.
3) Check if post belongs to disabled category
We are using array_intersect function to check if there is any intersection between the $my_post_cat and $disable_cat array. If there is an intersection , e can safely assume that the post belongs to one or more disabled categories.
4) Run appropriate filter:
In case the post belongs to a disabled category we will close the comments by hooking to the comments_open filter.
We will hook two functions, st_close_comments_on_category and st_deregister_reply_js. The role of these functions is to mark comments as closed and De-register the comment-reply.js respectively.
Well thats it. This snippet is quite straight forward. Simply paste it in your functions.php file and you are good to go.
Use a Plugin
For those of you who are not comfortable with editing code, I recently released a plugin to do the same.
Go to Settings -> Disable Comments on Post Categories and check the categories for which you want to disable comments.
Thats it. The plugin will now ensure that comments are closed on Posts of disabled categories.
If you have any questions then feel free to ask in the comments section 🙂
- How to create 301 Redirects in WordPress - December 21, 2019
- How to Filter Blog Posts by Author in WordPress Admin panel - October 21, 2019
- How to convert / change post type in wordpress - October 21, 2019
Thank you, it really works without installing as plugin
It really worked. Thanks a lot!
Thank you
it is working
Thanks buddy .. this worked awesome.
Very helpful. Thankyou.
Thank you. Your directions and screenshots were very clear and easy to follow and it helped me to disable post comments on my site. I am grateful for your help.
Thanks, it works perfectly well.