The get_default_comment_status WordPress PHP filter allows you to modify the default comment status for a given post type.
Usage
add_filter('get_default_comment_status', 'your_function_name', 10, 3);
function your_function_name($status, $post_type, $comment_type) {
// your custom code here
return $status;
}
Parameters
$status(string) – Default status for the given post type, either ‘open’ or ‘closed’.$post_type(string) – Post type. Default is ‘post’.$comment_type(string) – Type of comment. Default is ‘comment’.
More information
See WordPress Developer Resources: get_default_comment_status
Examples
Close comments on all pages
Prevent comments on all pages by default.
add_filter('get_default_comment_status', 'close_comments_on_pages', 10, 3);
function close_comments_on_pages($status, $post_type, $comment_type) {
if ($post_type == 'page') {
$status = 'closed';
}
return $status;
}
Open comments on custom post type
Enable comments by default on a custom post type called ‘event’.
add_filter('get_default_comment_status', 'open_comments_on_events', 10, 3);
function open_comments_on_events($status, $post_type, $comment_type) {
if ($post_type == 'event') {
$status = 'open';
}
return $status;
}
Close trackbacks and pingbacks
Disable trackbacks and pingbacks by default for all post types.
add_filter('get_default_comment_status', 'close_trackbacks_and_pingbacks', 10, 3);
function close_trackbacks_and_pingbacks($status, $post_type, $comment_type) {
if ($comment_type == 'trackback' || $comment_type == 'pingback') {
$status = 'closed';
}
return $status;
}
Open comments on a specific category
Allow comments by default on posts within the ‘featured’ category.
add_filter('get_default_comment_status', 'open_comments_on_featured_category', 10, 3);
function open_comments_on_featured_category($status, $post_type, $comment_type) {
if ($post_type == 'post' && has_category('featured')) {
$status = 'open';
}
return $status;
}
Close comments on password protected posts
Disable comments by default on password protected posts.
add_filter('get_default_comment_status', 'close_comments_on_protected_posts', 10, 3);
function close_comments_on_protected_posts($status, $post_type, $comment_type) {
if (post_password_required()) {
$status = 'closed';
}
return $status;
}