Advertisement
sierre

Turn Off Comment / Completely Turn Off Commenting

Dec 4th, 2024 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2. // Disable support for comments and trackbacks in post types
  3. function disable_comments_post_types_support() {
  4.     $post_types = get_post_types();
  5.     foreach ($post_types as $post_type) {
  6.         if (post_type_supports($post_type, 'comments')) {
  7.             remove_post_type_support($post_type, 'comments');
  8.             remove_post_type_support($post_type, 'trackbacks');
  9.         }
  10.     }
  11. }
  12. add_action('admin_init', 'disable_comments_post_types_support');
  13.  
  14. // Close comments on the front-end
  15. function disable_comments_status() {
  16.     return false;
  17. }
  18. add_filter('comments_open', 'disable_comments_status', 20, 2);
  19. add_filter('pings_open', 'disable_comments_status', 20, 2);
  20.  
  21. // Hide existing comments
  22. function disable_comments_hide_existing($comments) {
  23.     return [];
  24. }
  25. add_filter('comments_array', 'disable_comments_hide_existing', 10, 2);
  26.  
  27. // Remove comments page from the admin 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 the 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 the 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');*/
  56.  
  57. // If Unable to see the Comments from the admin menu:
  58. /*function restore_comments_menu() {
  59.     add_menu_page(
  60.         'Comments',
  61.         'Comments',
  62.         'edit_posts',
  63.         'edit-comments.php',
  64.         '',
  65.         'dashicons-admin-comments',
  66.         25
  67.     );
  68. }
  69. add_action( 'admin_menu', 'restore_comments_menu' );*/
  70.  
  71. // If you or a plugin has altered the admin bar to remove the Comments menu, you can restore it with this snippet:
  72. /*function restore_admin_bar_comments( $wp_admin_bar ) {
  73.     if ( current_user_can( 'edit_posts' ) ) {
  74.         $wp_admin_bar->add_node( array(
  75.             'id'    => 'comments',
  76.             'title' => 'Comments',
  77.             'href'  => admin_url( 'edit-comments.php' ),
  78.         ));
  79.     }
  80. }
  81. add_action( 'admin_bar_menu', 'restore_admin_bar_comments', 100 );*/
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement