How to Disable All Comments on WordPress Using a Snippet
If you run a WordPress site, you may need to completely disable comments. Whether to avoid spam or simply because comments are not relevant to your site, WordPress makes it easy to do this with a snippet.
What is a snippet?
A snippet is a small piece of code that you can add to your WordPress site to modify or extend its functionalities. Instead of installing a plugin for every small change, you can use snippets to quickly add specific functions without overloading your site. These pieces of code are generally placed in your theme’s functions.php
file.
Snippet to Disable Comments on WordPress
Here’s a snippet that allows you to disable all comments on your WordPress site and remove all related options from the admin panel:
// Disable comments on posts
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Remove existing comments from admin
add_action('admin_menu', function() {
remove_menu_page('edit-comments.php');
});
// Remove comments widget from dashboard
add_action('wp_dashboard_setup', function() {
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
});
// Hide "Discussion" options in post/page editing screens
add_action('admin_init', function() {
remove_meta_box('commentstatusdiv', 'post', 'normal');
remove_meta_box('commentstatusdiv', 'page', 'normal');
});
// Remove "Comments" from admin bar
add_action('wp_before_admin_bar_render', function() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('comments');
});
Visited 1 times, 1 visit(s) today