5

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?

5 Answers 5

4

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

1
  • 1
    Please, notice that to disable the metabox for the CPT you need to write the CPT name instead of 'page' or 'post'. For instance, if your CPT is 'book' then it would be remove_meta_box( 'commentstatusdiv', 'book', 'normal' ); Jan 3, 2019 at 17:37
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');
2
  • 1
    problem with this is that it doesn't remove it completely. if you pull down the screen options you can recheck it and then it displays again. I'm looking more for a way to force the comments to be allowed while I remove that metabox through the 'supports' option in the custom post type.
    – JonnyPlow
    Oct 5, 2011 at 19:00
  • If you remove the comments through the 'supports' option in the custom post type you remove the comments all together. you can try using remove_meta_box( 'commentstatusdiv' , 'page' , 'normal' );
    – Bainternet
    Oct 5, 2011 at 19:08
1

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>';
    };
  }
);
0

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');
1
  • problem with this is that it doesn't remove it completely. if you pull down the screen options you can recheck it and then it displays again. I'm looking more for a way to force the comments to be allowed while I remove that metabox through the 'supports' option in the custom post type.
    – JonnyPlow
    Oct 5, 2011 at 18:59
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.