Hide comment form completely (preferably without using CSS)

I need to conditionally not show the comment form.

I’ve added a filter to comment_form_defaults as follows:

Read More
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.

Related posts

Leave a Reply

1 comment

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