WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

protected by Community Jun 30 '11 at 9:29

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

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.