If you want to disable comments in WordPress entirely, whether for security reasons, performance, or to keep your site cleaner, the following PHP snippet provides a complete solution. This code removes all traces of comments from both the WordPress admin panel and the front-end.
<?php
add_action('admin_init', function () {
// Redirect users trying to access comment-related pages
global $current_page;
if ($current_page === 'edit-comments.php' || $current_page === 'options-discussion.php') {
wp_redirect(admin_url());
exit;
}
// Remove comment widget from the dashboard
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Disable comment and trackback support for all post types
foreach (get_post_types() as $content_type) {
if (post_type_supports($content_type, 'comments')) {
remove_post_type_support($content_type, 'comments');
remove_post_type_support($content_type, 'trackbacks');
}
}
});
// Prevent comments from being opened on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments from displaying
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comment menu and discussion settings from the admin panel
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
remove_submenu_page('options-general.php', 'options-discussion.php');
});
// Eliminate comment section from the admin toolbar
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
// Remove comments icon from the WordPress admin bar
function remove_admin_bar_comment_icon() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('comments');
}
add_action('wp_before_admin_bar_render', 'remove_admin_bar_comment_icon');
What Does This Code Do?
- Redirects Users from Comment Pages
- If an admin tries to access the comments management page (
edit-comments.php) or discussion settings page (options-discussion.php), they are redirected to the dashboard.
- If an admin tries to access the comments management page (
- Removes Comments Metabox from the Dashboard
- The “Recent Comments” widget is removed from the WordPress dashboard.
- Disables Comments and Trackbacks
- Removes support for comments and trackbacks on all post types.
- Closes Comments on Front-End
- The
comments_openandpings_openfilters are set tofalse, preventing new comments from being added.
- The
- Hides Existing Comments
- The
comments_arrayfilter is set to return an empty array, effectively hiding all previously posted comments.
- The
- Removes Comments Menu Items
- The “Comments” menu in the admin panel is removed.
- The discussion settings submenu under “Settings” is removed.
- Removes Comment Links from the Admin Bar
- The comment bubble icon in the top admin bar is removed.
How to Use It
Simply add this code to your theme’s functions.php file or include it in a custom plugin to disable comments completely on your WordPress site.
By implementing this code, your WordPress site will function as if the comments feature never existed, providing a cleaner and distraction-free experience for both admins and users.