Remove Comments Metabox but still allow comments

I already know how to remove a metabox from my custom post type edit page. However I want to remove the comments metabox but still allow commenting for the post. Because I notice when I do remove it, it disables comments. Any function I can use?

Related posts

Leave a Reply

5 comments

  1. Don’t remove this via CSS. The _POST part is also active and WP save the data!
    Use the hooks to remove meta boxes; code by scratch.

    function fb_remove_comments_meta_boxes() {
        remove_meta_box( 'commentstatusdiv', 'post', 'normal' );
        remove_meta_box( 'commentstatusdiv', 'page', 'normal' );
        // remove trackbacks
        remove_meta_box( 'trackbacksdiv', 'post', 'normal' );
        remove_meta_box( 'trackbacksdiv', 'page', 'normal' );
    }
    add_action( 'admin_init', 'fb_remove_comments_meta_boxes' );
    

    see more on a plugin to remove all UI-elements and function for comments: https://github.com/bueltge/Remove-Comments-Absolutely

  2. You can use the UI to remove it:

    Click on the “Screen Options” at the top right side of the edit screen

    enter image description here

    and uncheck the Discussion check box

    enter image description here

    or if you want to do it by code simply hide the container div by style="display:none;"

    function hide_comments_div() {
    global $pagenow;
    if ($pagenow=='post-new.php' OR $pagenow=='post.php')
            echo '<style>#commentstatusdiv{ display:none; }</style>';
    }
    add_action('admin_head', 'hide_comments_div');
    
  3. There is problem in file /wp-includes/post.php function wp_insert_post()

    if ( empty($comment_status) ) {
     if ( $update )
      $comment_status = 'closed';
     else
       $comment_status = get_option('default_comment_status');
    }
    

    Your comments will be close after update. The solution is change callback of commentstatusdiv insted:

    add_action(
     'add_meta_boxes', function () {
        global $wp_meta_boxes, $current_screen;
        $wp_meta_boxes[$current_screen->id]['normal']['core']['commentstatusdiv']['callback'] = function () {
            global $post;
            echo '<input type="hidden" value="' . $post->comment_status . '"      name="comment_status"/>';
            echo '<input type="hidden" value="' . $post->ping_status . '" name="ping_status"/>';
            echo '<style type="text/css">#commentstatusdiv {display: none;}</style>';
        };
      }
    );
    
  4. Add this in functions.php of your theme

    function tune_admin_area() {
            echo '<style>#commentstatusdiv{ display:none; }</style>';
    }
    add_action('admin_head', 'tune_admin_area');
    
  5. This is what I used to hide some of the meta boxes, including the comment status box:

    if (is_admin()) :
        function my_remove_meta_boxes() {
            if( !current_user_can('manage_options') ) {
                remove_meta_box('postcustom', 'post', 'normal');
                remove_meta_box('trackbacksdiv', 'post', 'normal');
                remove_meta_box('commentstatusdiv', 'post', 'normal');
                remove_meta_box('slugdiv', 'post', 'normal');
            }
        }
        add_action( 'admin_menu', 'my_remove_meta_boxes' );
    
        function handle_comments_setting( $data ) {
            if( !current_user_can('manage_options') ) {
                $data['comment_status'] = "open";
            }
            return $data;
        }
        add_filter( 'wp_insert_post_data', 'handle_comments_setting' );
    endif;
    

    This way the meta boxes will be hidden for all the contributors, but not the administrators.

    In the second function comment_status is set to "open" only when the same conditions are met that caused the comments to be disabled in the first place.