December 6, 2016 - No Comments!

How to remove WordPress comments without plugins

Would you remove WordPress comments without using plugins?

In this article I'll explain how to completely remove WordPress comments without installing any plugin.

One of the main advantages of WordPress is the presence of its plugins, which often resolve most of the needs without particular difficulties. Although surely the plugins can help, you should evaluate them individually, deciding in each case whether they are really useful or if you can solve your problem with few lines of code.

Please don't think I'm paranoid, I also use plugins (ex. these ones), but over the years I learned to select those really useful. Generally I prefer to exploit the potential of child themes and then implement all the functions for which the use of plugins might be excessive.

After this introduction I willingly share the functions that generally I use to remove WordPress comments. Of course, you've to paste these lines of code in your functions.php file.

First of all disable support for comments and trackbacks in post types.

function thmlv_disable_comments_post_types_support() {
	$post_types = get_post_types();
 	foreach ($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');
 		}
 	}
}
add_action('admin_init', 'thmlv_disable_comments_post_types_support');

Now close comments on the frontend (first function) and hide existing comments (second function).

function thmlv_disable_comments_status() {
	return false;
}
add_filter('comments_open', 'thmlv_disable_comments_status', 20, 2);
add_filter('pings_open', 'thmlv_disable_comments_status', 20, 2);

function thmlv_disable_comments_hide_existing_comments($comments) {
	$comments = array();
	return $comments;
}
add_filter('comments_array', 'thmlv_disable_comments_hide_existing_comments', 10, 2);

Remove now the comments page from admin menu

function thmlv_disable_comments_admin_menu() {
	remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'thmlv_disable_comments_admin_menu');

At this point you've to redirect any user trying to access comments page

function thmlv_disable_comments_admin_menu_redirect() {
	global $pagenow;
	if ($pagenow === 'edit-comments.php') {
		wp_redirect(admin_url()); exit;
	}
}
add_action('admin_init', 'thmlv_disable_comments_admin_menu_redirect');

Next step: Remove the metabox from dashboard

function thmlv_disable_comments_dashboard() {
	remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'thmlv_disable_comments_dashboard');

Last step! Remove the comments link from the admin bar

function thmlv_disable_comments_admin_bar() {
	if (is_admin_bar_showing()) {
		remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
	}
}
add_action('init', 'thmlv_disable_comments_admin_bar');

Now you're done, the comments are no longer active in your theme 🙂

Let me know if this article has been helpful

Published by: Themelovin in Code, Tips

Leave a Reply