WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/comment form

This page is marked as incomplete. You can help Codex by expanding it.

Description

This tag outputs a complete commenting form for use within a template.

Most strings and form fields may be controlled through the $args array passed into the function, while you may also choose to use the comment_form_default_fields filter to modify the array of default fields if you'd just like to add a new one or remove a single field. 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.

Please note, that although most parameters are marked as optional, not including them all in your code will produce errors when using define('WP_DEBUG', true);

Usage

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

Default Usage

<?php comment_form(); ?>

twentytendefault6.png

As seen in the popular twentyten theme - called here: wp-content/themes/twentyten/comments.php

Parameters

args
(array) (optional) Options for strings, fields etc in the form.
Default: (See below)
post_id
(mixed) (optional) Post ID to generate the form for, uses the current post if null
Default: null (the current post)

$args

Note: If you change the $defaults in your comments template using $new_defaults, you must declare the $new_defaults BEFORE you call comment_form($new_defaults);, otherwise, they won't take effect.

Default values:

fields
(array) (optional) Input fields: 'author', 'email', 'url', 'cookies'.
Default: apply_filters( 'comment_form_default_fields', $fields )
comment_field
(string) (optional) The textarea and the label of comment body.
Default:
'<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>'
must_log_in
(string) (optional)
Default:
'<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( ) ) ) ) . '</p>'
logged_in_as
(string) (optional)
Default:
'<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>'
comment_notes_before
(string) (optional) Text or HTML to be displayed before the set of comment form fields if the user is not logged in.
Default:
'<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>'
comment_notes_after
(string) (optional) Text or HTML to be displayed after the set of comment fields (and before the submit button)
Default:
'<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>'
id_form
(string) (optional) value of the id attribute of form element (<form> tag).
Default: 'commentform'
class_form
(string) (optional) value of the class attribute of form element (<form> tag).
Default: 'comment-form'
id_submit
(string) (optional) value of the id attribute of submit button.
Default: 'submit'
class_submit
(string) (optional) value of the class attribute of submit button.
Default: 'submit'
title_reply
(string) (optional) The title of comment form (when not replying to a comment, see comment_form_title).
Default: __( 'Leave a Reply' )
title_reply_to
(string) (optional) The title of comment form (when replying to a comment, see comment_form_title).
Default: __( 'Leave a Reply to %s' )
title_reply_before
(string) (optional) Text or HTML to be displayed before the reply title.
Default:
'<h3 id="reply-title" class="comment-reply-title">'
title_reply_after
(string) (optional) Text or HTML to be displayed after the reply title.
Default:
'</h3>'
cancel_reply_before
(string) (optional) Text or HTML to be displayed before the cancel reply link.
Default:
'<small>'
cancel_reply_after
(string) (optional) Text or HTML to be displayed after the cancel reply link.
Default:
'</small>'
cancel_reply_link
(string) (optional) link label to cancel reply.
Default: __( 'Cancel reply' )
label_submit
(string) (optional) the name of submit button.
Default: __( 'Post Comment' )

$fields

Default form fields:

$fields =  array(

  'author' =>
    '<p class="comment-form-author"><label for="author">' . __( 'Name', 'domainreference' ) .
    ( $req ? '<span class="required">*</span>' : '' ) . '</label>' .
    '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
    '" size="30"' . $aria_req . ' /></p>',

  'email' =>
    '<p class="comment-form-email"><label for="email">' . __( 'Email', 'domainreference' ) .
    ( $req ? '<span class="required">*</span>' : '' ) . '</label>' .
    '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
    '" size="30"' . $aria_req . ' /></p>',

  'url' =>
    '<p class="comment-form-url"><label for="url">' . __( 'Website', 'domainreference' ) . '</label>' .
    '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
    '" size="30" /></p>',
);

As of 4.9.6, if "Show comments cookies opt-in checkbox." is checked in Discussion Settings, a cookie consent checkbox will be added to the array:

$fields['cookies'] =  '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' />' . '<label for="wp-comment-cookies-consent">' . __( 'Save my name, email, and website in this browser for the next time I comment.' ) . '</label></p>';

Note: To use the variables present in the above code in a custom callback function, you must first set these variables within your callback using:

$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );

$consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';

Default $args array

$args = array(
  'id_form'           => 'commentform',
  'class_form'      => 'comment-form',
  'id_submit'         => 'submit',
  'class_submit'      => 'submit',
  'name_submit'       => 'submit',
  'title_reply'       => __( 'Leave a Reply' ),
  'title_reply_to'    => __( 'Leave a Reply to %s' ),
  'cancel_reply_link' => __( 'Cancel Reply' ),
  'label_submit'      => __( 'Post Comment' ),
  'format'            => 'xhtml',

  'comment_field' =>  '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) .
    '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true">' .
    '</textarea></p>',

  '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() ) )
    ) . '</p>',

  'logged_in_as' => '<p class="logged-in-as">' .
    sprintf(
    __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ),
      admin_url( 'profile.php' ),
      $user_identity,
      wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) )
    ) . '</p>',

  'comment_notes_before' => '<p class="comment-notes">' .
    __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) .
    '</p>',

  'comment_notes_after' => '<p class="form-allowed-tags">' .
    sprintf(
      __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ),
      ' <code>' . allowed_tags() . '</code>'
    ) . '</p>',

  'fields' => apply_filters( 'comment_form_default_fields', $fields ),
);

Return

void

Examples

Simple example how to change some comment form fields.

$comments_args = array(
        // change the title of send button 
        'label_submit'=>'Send',
        // change the title of the reply section
        'title_reply'=>'Write a Reply or Comment',
        // remove "Text or HTML to be displayed after the set of comment fields"
        'comment_notes_after' => '',
        // redefine your own textarea (the comment body)
        'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
);

comment_form($comments_args);

Uses filter hooks

Pluggable actions

Changelog

Source Code

comment_form() is located in wp-includes/comment-template.php.

Related

comments_template()

Comments Functions

See also index of Function Reference and index of Template Tags.