PHP 31
WP Disable Comments By micha on 12th February 2024 08:06:56 AM
  1. add_action('admin_init', function () {
  2.     // Redirect any user trying to access comments page
  3.     global $pagenow;
  4.      
  5.     if ($pagenow === 'edit-comments.php') {
  6.         wp_safe_redirect(admin_url());
  7.         exit;
  8.     }
  9.  
  10.     // Remove comments metabox from dashboard
  11.     remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
  12.  
  13.     // Disable support for comments and trackbacks in post types
  14.     foreach (get_post_types() as $post_type) {
  15.         if (post_type_supports($post_type, 'comments')) {
  16.             remove_post_type_support($post_type, 'comments');
  17.             remove_post_type_support($post_type, 'trackbacks');
  18.         }
  19.     }
  20. });
  21.  
  22. // Close comments on the front-end
  23. add_filter('comments_open', '__return_false', 20, 2);
  24. add_filter('pings_open', '__return_false', 20, 2);
  25.  
  26. // Hide existing comments
  27. add_filter('comments_array', '__return_empty_array', 10, 2);
  28.  
  29. // Remove comments page in menu
  30. add_action('admin_menu', function () {
  31.     remove_menu_page('edit-comments.php');
  32. });
  33.  
  34. // Remove comments links from admin bar
  35. add_action('init', function () {
  36.     if (is_admin_bar_showing()) {
  37.         remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
  38.     }
  39. });

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.