Turn off comments everywhere
Closes comments and pingbacks on every post and page, hides existing comments, and removes Comments from the admin menu, the admin bar and the dashboard. WooCommerce product reviews keep working.
<?php
// Post types that keep their comments (WooCommerce reviews are comments).
$keep = array( 'product' );
add_filter( 'comments_open', static fn( $open, $post_id ) => in_array( get_post_type( $post_id ), $keep, true ) ? $open : false, 20, 2 );
add_filter( 'pings_open', static fn( $open, $post_id ) => in_array( get_post_type( $post_id ), $keep, true ) ? $open : false, 20, 2 );
add_filter( 'comments_array', static fn( $comments, $post_id ) => in_array( get_post_type( $post_id ), $keep, true ) ? $comments : array(), 20, 2 );
add_filter( 'feed_links_show_comments_feed', '__return_false' );
add_action(
'init',
static function () use ( $keep ) {
foreach ( get_post_types() as $post_type ) {
if ( ! in_array( $post_type, $keep, true ) && post_type_supports( $post_type, 'comments' ) ) {
remove_post_type_support( $post_type, 'comments' );
remove_post_type_support( $post_type, 'trackbacks' );
}
}
},
100
);
add_action(
'admin_menu',
static function () {
remove_menu_page( 'edit-comments.php' );
remove_submenu_page( 'options-general.php', 'options-discussion.php' );
},
100
);
add_action(
'admin_init',
static function () {
global $pagenow;
if ( in_array( $pagenow, array( 'edit-comments.php', 'options-discussion.php' ), true ) ) {
wp_safe_redirect( admin_url() );
exit;
}
}
);
add_action(
'wp_dashboard_setup',
static function () {
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
);
add_action(
'admin_bar_menu',
static function ( $admin_bar ) {
$admin_bar->remove_node( 'comments' );
},
100
);
In these packs
- Turn off what you don’t use: Each of these replaces a small plugin: no comments, classic editor, no feeds, no search and more.