Aurangajeb

Disable WP comments / discussion

Jun 9th, 2022
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | None | 0 0
  1. ##################################
  2. //Disable WP comments / discussion
  3. ##################################
  4.  
  5. add_action('admin_init', function () {
  6.     // Redirect any user trying to access comments page
  7.     global $pagenow;
  8.    
  9.     if ($pagenow === 'edit-comments.php') {
  10.         wp_redirect(admin_url());
  11.         exit;
  12.     }
  13.  
  14.     // Remove comments metabox from dashboard
  15.     remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
  16.  
  17.     // Disable support for comments and trackbacks in post types
  18.     foreach (get_post_types() as $post_type) {
  19.         if (post_type_supports($post_type, 'comments')) {
  20.             remove_post_type_support($post_type, 'comments');
  21.             remove_post_type_support($post_type, 'trackbacks');
  22.         }
  23.     }
  24. });
  25.  
  26. // Close comments on the front-end
  27. add_filter('comments_open', '__return_false', 20, 2);
  28. add_filter('pings_open', '__return_false', 20, 2);
  29.  
  30. // Hide existing comments
  31. add_filter('comments_array', '__return_empty_array', 10, 2);
  32.  
  33. // Remove comments page in menu
  34. add_action('admin_menu', function () {
  35.     remove_menu_page('edit-comments.php');
  36. });
  37.  
  38. // Remove comments links from admin bar
  39. add_action('init', function () {
  40.     if (is_admin_bar_showing()) {
  41.         remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
  42.     }
  43. });
Advertisement
Add Comment
Please, Sign In to add comment