Display Comment Form With Ease In WordPress 3.0

Display Comment Form With Ease In WordPress 3.0
Display Comment Form With Ease In WordPress 3.0

The PHP code for comment form (in comments.php file) in WordPress is usually complicated, and could makes WordPress beginners confused to use and customize. Fortunately, this is changed in WordPress 3.0. WordPress 3.0 provides a new function comment_form() to display comment form without any effort.

To use this function, just open the comments.php file of your theme, find the old-fashioned code for displaying comment form, it looks like this:

<div id="respond">
    <h3><?php comment_form_title('Leave a Reply', 'Leave a Reply to %s'); ?></h3>

    <div id="cancel-comment-reply"><?php cancel_comment_reply_link() ?></div>
</div><!-- #respond -->

Replace it with the following code:

<?php comment_form(); ?>

The function comment_form() has 2 parameters, all of them are optional:

<?php comment_form($args, $post_id); ?>

$args: Options for strings, fields etc in the form
$post_id: Post ID to generate the form for, uses the current post if null

The $arg parameter has the default value:

<?php $defaults = array( 'fields' => apply_filters( 'comment_form_default_fields', array(
    'author' => '<p class="comment-form-author">' .
                '<label for="author">' . __( 'Name' ) . '</label> ' .
                ( $req ? '<span class="required">*</span>' : '' ) .
                '<input id="author" name="author" type="text" value="' .
                esc_attr( $commenter['comment_author'] ) . '" size="30" tabindex="1"' . $aria_req . ' />' .
                '</p><!-- #form-section-author .form-section -->',
    'email'  => '<p class="comment-form-email">' .
                '<label for="email">' . __( 'Email' ) . '</label> ' .
                ( $req ? '<span class="required">*</span>' : '' ) .
                '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" tabindex="2"' . $aria_req . ' />' .
                '</p><!-- #form-section-email .form-section -->',
    'url'    => '<p class="comment-form-url">' .
                '<label for="url">' . __( 'Website' ) . '</label>' .
                '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" tabindex="3" />' .
                '</p><!-- #form-section-url .form-section -->' ) ),
    'comment_field' => '<p class="comment-form-comment">' .
                '<label for="comment">' . __( 'Comment' ) . '</label>' .
                '<textarea id="comment" name="comment" cols="45" rows="8" tabindex="4" aria-required="true"></textarea>' .
                '</p><!-- #form-section-comment .form-section -->',
    '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' => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%s">%s</a>. <a href="%s" title="Log out of this account">Log out?</a></p>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ),
    'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email is <em>never</em> published nor shared.' ) . ( $req ? __( ' Required fields are marked <span class="required">*</span>' ) : '' ) . '</p>',
    'comment_notes_after' => '<dl class="form-allowed-tags"><dt>' . __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes:' ) . '</dt> <dd><code>' . allowed_tags() . '</code></dd></dl>',
    'id_form' => 'commentform',
    'id_submit' => 'submit',
    'title_reply' => __( 'Leave a Reply' ),
    'title_reply_to' => __( 'Leave a Reply to %s' ),
    'cancel_reply_link' => __( 'Cancel reply' ),
    'label_submit' => __( 'Post Comment' ),
); ?>

The parameters of $arg are self-defined by their names. If you’re confused with these values, you can leave $arg as null.

Another way to customize $arg instead of write full long code is using the comment_form_default_fields filter. For example, if you want to hide the URL field, just put the following code into functions.php file:

add_filter('comment_form_default_fields', 'mytheme_remove_url');

function mytheme_remove_url($arg) {
    $arg['url'] = '';
    return $arg;

All fields are also individually passed through a filter of the form comment_form_field_$name where $name is the key used in the array of fields. So, we can hide the URL field in another way like this:

add_filter('comment_form_field_url', 'mytheme_remove_url');

function mytheme_remove_url($url) {
    return '';

Using comment_form() function is a good way to keep your template code clean and easy to maintain. It does not reduce the customization of comment form, it even provides more ways to customize it via bunch of filters. If you’re using WordPress 3.0, I think you definitely should use this function in replacement of old code.

10 thoughts on “Display Comment Form With Ease In WordPress 3.0

  1. Great tips though, i am afraid that replacing this above said file could corrupt my theme. So can you help me through. I need a very good looking comment box.

  2. I have a question regarding this new way of handling comments…

    Say I put “comment_form();” in my comments.php file…

    And then in single.php I put “comment_form();” before “php endwhile()” – despite the comment form showing up on the page that’s rendered via single.php, there are no comments (though the post is supposed to have comments). I don’t know what to do. Can you offer any help?

Comments are closed.

Meta Box - The most lightweight yet powerful WordPress custom fields plugin Get It Now