Are comments helpful on some websites?
Comments can be helpful on some websites, allowing visitors to share their thoughts and interact. However, not every site needs or wants comments. For business websites, portfolios, or other content where interaction isn’t necessary, comments can lead to spam, extra moderation work, and security concerns.
If you want to remove comments entirely, you can disable comments in WordPress without using a plugin. While there are plugins that can do this, using a bit of custom code is a more efficient and lightweight solution. The following code completely disables comments on your WordPress site, making sure that both the ability to post comments and any existing comments are removed from view.
This guide will explain why you might want to disable comments and how to easily do it with this code.
First Code
Below is just one of examples of the code that you can use immediately to disable comments in your WordPress website:
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_safe_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('admin_bar_menu', function () {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}, 0);
Purpose of the Code
The code shared above is designed to completely disable comments on a WordPress site, including:
- Backend Access: Redirecting any user who tries to access the comments page in the admin area.
- Dashboard Clean-up: Removing the comments metabox from the WordPress dashboard.
- Post Type Support: Disabling support for comments and trackbacks across all post types.
- Frontend Comments: Closing comments and pings on the front-end, effectively preventing any new comments.
- Hiding Existing Comments: Preventing existing comments from displaying on posts.
- Admin Menu: Removing the comments page from the WordPress admin menu.
- Admin Bar: Removing the comments link from the admin bar.
Importance of disabling comments in WordPress
- You want to disable comments site-wide without relying on a plugin.
- You don't want to deal with comment moderation, spam, or any user-generated content via comments.
- Your site doesn’t require interaction through comments, such as for a business site, portfolio, or any content where comments aren't relevant.
Correctness and Revisions
- Admin Bar Cleanup: The code to remove the comments link from the admin bar could be adjusted for better readability:
add_action('admin_bar_menu', function ($wp_admin_bar) {
$wp_admin_bar->remove_node('comments');
}, 999);
This approach uses the $wp_admin_bar
object, which is a bit more robust.
- Compatibility: To ensure maximum compatibility, you might want to add an
if (function_exists(...))
check around each action or filter to avoid any issues if WordPress changes its internal APIs in future updates. - Functionality Expansion: If you want to remove comment-related widgets as well, you could add this:
add_action('widgets_init', function () {
unregister_widget('WP_Widget_Recent_Comments');
});
- Plugin and Theme Compatibility: Ensure that your theme or plugins don’t re-enable comments or conflict with this code. If you notice any conflicts, you may need to further customize this code.
Conclusion
The code is solid and does what it’s intended to do: disable all aspects of comments on a WordPress site. The minor revisions I suggested can improve readability and robustness but aren’t strictly necessary.