Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 1 down vote accepted

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', 'remove_comments' );

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

share|improve this answer

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');
share|improve this answer
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 '11 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 '11 at 19:08

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>';
    };
  }
);
share|improve this answer

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');
share|improve this answer
    
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 '11 at 18:59

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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