Disable comments in WordPress without a plugin
Switch off comments on your whole site with one Snipfire snippet. An honest look at what it shares with the Disable Comments plugin and what it leaves out.
What Disable Comments does
Disable Comments turns comments off on the post types you choose and hides comment features across the admin. The plugin on WordPress.org
What the snippet does the same
- Comments and pingbacks are closed on posts, pages and other post types, and existing comments are hidden on the site.
- Comments disappears from the admin menu, the admin bar and the dashboard, and Settings > Discussion is hidden.
- The comment boxes and settings leave the post edit screens.
What's different
- The plugin lets you choose which post types lose comments from a settings screen. The snippet switches them off everywhere except WooCommerce products, so product reviews keep working. To change that, you edit a list in the code.
- The plugin has a tool to delete existing comments. The snippet only hides them; they stay in the database.
- The plugin redirects comment feed addresses to the post. The snippet only removes the comment feed link from the page head.
- The plugin also offers multisite network settings, role exceptions and WP-CLI commands. The snippet has none of these.
When to keep the plugin Keep the plugin if you want comments on some post types, need to bulk delete old comments, or manage a multisite network.
The snippet: 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
);
Notes, where it runs and what it was tested with
Switching from the plugin
- Install the snippet in Snipfire (it arrives switched off).
- Deactivate Disable Comments.
- Switch the snippet on. Snipfire checks the code and test-loads your site first.
- Check the page it affects, then delete the plugin.