Skip to content

Instantly share code, notes, and snippets.

@mattclements
Last active October 29, 2024 22:04
Show Gist options
  • Save mattclements/eab5ef656b2f946c4bfb to your computer and use it in GitHub Desktop.
Save mattclements/eab5ef656b2f946c4bfb to your computer and use it in GitHub Desktop.
Wordpress Disable Comments (add to function.php)
<?php
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
// Remove comments metabox from dashboard
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Disable support for comments and trackbacks in post types
foreach (get_post_types() as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
});
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
// Remove comments links from admin bar
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
@jack-fdrv
Copy link

jack-fdrv commented May 3, 2022

image
We also need to hide this info.

@amarineau
Copy link

Nice ! Thx :)

@masterewot
Copy link

I need this for my site

@drnickyoung
Copy link

drnickyoung commented Dec 19, 2022

@jack-fdrv You may have already found an answer but thought I'd comment in case. This does depend on the theme used but the following code works in many cases.

// Return a comment count of zero to hide existing comment entry link.
function zero_comment_count($count){
     return 0;
}
add_filter('get_comments_number', 'zero_comment_count');

@shanemac10
Copy link

Here's the collective code from this thread to date, as it worked for me...

<?php

add_action('admin_init', function () {
	// Redirect any user trying to access comments page
	global $pagenow;

	if ($pagenow === 'edit-comments.php' || $pagenow === 'options-discussion.php') {
		wp_redirect(admin_url());
		exit;
	}

	// Remove comments metabox from dashboard
	remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

	// Disable support for comments and trackbacks in post types
	foreach (get_post_types() as $post_type) {
		if (post_type_supports($post_type, 'comments')) {
			remove_post_type_support($post_type, 'comments');
			remove_post_type_support($post_type, 'trackbacks');
		}
	}
});

// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);

// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);

// Remove comments page in menu
add_action('admin_menu', function () {
  remove_menu_page('edit-comments.php');
	remove_submenu_page('options-general.php', 'options-discussion.php');
});

// Remove comments links from admin bar
add_action('init', function () {
	if (is_admin_bar_showing()) {
		remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
	}
});

// Remove comments icon from admin bar
add_action('wp_before_admin_bar_render', function() {
	global $wp_admin_bar;
	$wp_admin_bar->remove_menu('comments');
});

// Return a comment count of zero to hide existing comment entry link.
function zero_comment_count($count){
	return 0;
}
add_filter('get_comments_number', 'zero_comment_count');

// Multisite - Remove manage comments from admin bar
add_action( 'admin_bar_menu', 'remove_toolbar_items', PHP_INT_MAX -1 );
function remove_toolbar_items( $bar )
{
	$sites = get_blogs_of_user( get_current_user_id() );
	foreach ( $sites as $site )
	{
		$bar->remove_node( "blog-{$site->userblog_id}-c" );
	}
}

I put mine in a separate file disable-comments.php and included it in functions.php, to make it more portable moving forward...

// Disable Comments
include('inc/disable-comments.php');

@dave-slaughter
Copy link

Nice work by this thread, so here's an additional improvement you might like.

I noticed 5 queries on wp_comments were running for each admin page, which can be removed. I put this in the "init" hook where I already have code inside an is_admin() test, but you don't need this test. Also, the filter can't go in the "admin_init" hook as that is too late as the queries will have already run.

add_action('init', function () {
	if (is_admin()) {
		// stop queries to count comments, which are called when the admin menu is displayed
		add_filter('wp_count_comments', function() {
			return (object) [
				'approved'       => 0,
				'moderated'      => 0,
				'spam'           => 0,
				'trash'          => 0,
				'post-trashed'   => 0,
				'total_comments' => 0,
				'all'            => 0,
			];
		});
	}
});

@OzzyCzech
Copy link

You can disable comment feed with:

add_filter('feed_links_show_comments_feed', '__return_false');

@EarthlingDavey
Copy link

To hide the default first comment from the activity feed

add_filter( 'the_comments', '__return_empty_array' );

also

// Return a comment count of zero to hide existing comment entry link.
function zero_comment_count($count){
	return 0;
}
add_filter('get_comments_number', 'zero_comment_count');

can be shortened to:

add_filter('get_comments_number', '__return_zero');

Thanks folks :)

@janez33
Copy link

janez33 commented Jul 30, 2024

It's better to use this code taken from plugin Disable Comments (mu plugin version). Most up-to-date code.
https://gist.github.com/doiftrue/ab931c1d866cb113b4ff318a5faeb3b3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment