PHPwordpressbeginner
Disable WordPress Comments Globally
Completely disable comments across your entire WordPress site
Faisal Yaqoob
November 1, 2025
#wordpress#comments#disable#spam#optimization
Code
php
1 // Disable support for comments and trackbacks in post types 2 function disable_comments_post_types_support() { 3 $post_types = get_post_types(); 4 foreach ($post_types as $post_type) { 5 if (post_type_supports($post_type, 'comments')) { 6 remove_post_type_support($post_type, 'comments'); 7 remove_post_type_support($post_type, 'trackbacks'); 8 } 9 } 10 } 11 add_action('admin_init', 'disable_comments_post_types_support'); 12
13 // Close comments on the front-end 14 function disable_comments_status() { 15 return false; 16 } 17 add_filter('comments_open', 'disable_comments_status', 20, 2); 18 add_filter('pings_open', 'disable_comments_status', 20, 2); 19
20 // Hide existing comments 21 function disable_comments_hide_existing_comments($comments) { 22 $comments = array(); 23 return $comments; 24 } 25 add_filter('comments_array', 'disable_comments_hide_existing_comments', 10, 2); 26
27 // Remove comments page in menu 28 function disable_comments_admin_menu() { 29 remove_menu_page('edit-comments.php'); 30 } 31 add_action('admin_menu', 'disable_comments_admin_menu'); 32
33 // Redirect any user trying to access comments page 34 function disable_comments_admin_menu_redirect() { 35 global $pagenow; 36 if ($pagenow === 'edit-comments.php') { 37 wp_redirect(admin_url()); 38 exit; 39 } 40 } 41 add_action('admin_init', 'disable_comments_admin_menu_redirect'); 42
43 // Remove comments metabox from dashboard 44 function disable_comments_dashboard() { 45 remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal'); 46 } 47 add_action('admin_init', 'disable_comments_dashboard'); 48
49 // Remove comments links from admin bar 50 function disable_comments_admin_bar() { 51 if (is_admin_bar_showing()) { 52 remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60); 53 } 54 } 55 add_action('init', 'disable_comments_admin_bar');
Disable WordPress Comments Globally
This snippet completely disables comments across your WordPress site, removes comment-related admin menu items, and hides comment forms from the frontend.
// Disable support for comments and trackbacks in post types
function 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', 'disable_comments_post_types_support');
// Close comments on the front-end
function disable_comments_status() {
return false;
}
add_filter('comments_open', 'disable_comments_status', 20, 2);
add_filter('pings_open', 'disable_comments_status', 20, 2);
// Hide existing comments
function disable_comments_hide_existing_comments($comments) {
$comments = array();
return $comments;
}
add_filter('comments_array', 'disable_comments_hide_existing_comments', 10, 2);
// Remove comments page in menu
function disable_comments_admin_menu() {
remove_menu_page('edit-comments.php');
}
add_action('admin_menu', 'disable_comments_admin_menu');
// Redirect any user trying to access comments page
function disable_comments_admin_menu_redirect() {
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
}
add_action('admin_init', 'disable_comments_admin_menu_redirect');
// Remove comments metabox from dashboard
function disable_comments_dashboard() {
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
}
add_action('admin_init', 'disable_comments_dashboard');
// Remove comments links from admin bar
function disable_comments_admin_bar() {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
}
add_action('init', 'disable_comments_admin_bar');
Features
- Complete Removal: Disables comments site-wide
- Admin Cleanup: Removes comment-related menu items
- Security: Prevents access to comment admin pages
- Performance: Reduces database queries
- Clean UI: Hides all comment-related elements
Alternative: Disable for Specific Post Types
// Disable comments only for pages and custom post types
function disable_comments_specific_post_types() {
$post_types = array('page', 'custom_post_type');
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', 'disable_comments_specific_post_types');
Related Snippets
WordPress Custom Image Sizes
Add custom image sizes and regenerate thumbnails automatically
PHPwordpressbeginner
phpPreview
// Add custom image sizes
function add_custom_image_sizes() {
// Hero image
add_image_size('hero-large', 1920, 800, true); // Hard crop
...#wordpress#images#thumbnails+2
11/10/2025
View
WordPress Custom Breadcrumbs
Create SEO-friendly breadcrumb navigation without plugins
PHPwordpressintermediate
phpPreview
// Display breadcrumbs
function custom_breadcrumbs() {
// Settings
$separator = ' » ';
...#wordpress#breadcrumbs#navigation+2
11/15/2025
View
Create Custom WordPress REST API Endpoint
Add custom REST API endpoints to extend WordPress functionality
PHPwordpressintermediate
phpPreview
// Register custom REST API endpoint
function register_custom_api_endpoint() {
register_rest_route('custom/v1', '/posts', array(
'methods' => 'GET',
...#wordpress#rest-api#api+2
11/14/2025
View