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 need to conditionally not show the comment form.

I've added a filter to comment_form_defaults as follows:

function dont_show_comments($defaults) {
  $defaults = array(
    'fields'               => array(),
    'comment_field' => '',
    'must_log_in'          => '<p class="must-log-in">' .  sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
    'logged_in_as'         => '',
    'comment_notes_before' => '',
    'comment_notes_after'  => '',
    'id_form'              => 'commentform',
    'id_submit'            => 'hide',
    'title_reply'          => __( 'You need to have accepted the terms to leave a reply' ),
    'title_reply_to'       => __( 'Leave a Reply %s' ),
    'cancel_reply_link'    => __( 'Cancel reply' ),
    'label_submit'         => '',
   );

return $defaults;
}

This mostly works, but I'm left with an unlabelled button (the submit button).

What's the best way of not showing the form at all - preferably via a hook, and preferably not using CSS.

share|improve this question
    
Are you trying to disable the comments form throughout the entire blog? –  VicePrez May 24 '11 at 9:11
    
No - just for a particular session for a particular condition –  anu May 24 '11 at 9:23
add comment

1 Answer

up vote 3 down vote accepted

I can think of only following ways to achieve this.

  1. There is a filter "comments_open" that check if the post whose $post_id is provided has comments open. You can use that to return false.
  2. There is another filter "comments_template" that return the template file to be used to display comment form. You can return an empty file and hence no comment form will be displayed.
  3. The other is to set the comment_status on $post object to close before comment form is displayed, it will stop theme from displaying the comment form even if they use the hardcoded comment form.
share|improve this answer
    
using the 'comments_open' hook works great. –  anu May 24 '11 at 11:56
add comment

protected by Community Jun 30 '11 at 9:29

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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