Languages: English • 日本語 (Add your language)
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);
<?php comment_form( $args, $post_id ); ?>
<?php comment_form(); ?>
As seen in the popular twentyten theme - called here: wp-content/themes/twentyten/comments.php
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:
apply_filters( 'comment_form_default_fields', $fields )
'<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>'
'<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>'
'<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>'
'<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>'
'<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
attribute of form
element (<form>
tag).
class
attribute of form
element (<form>
tag).
id
attribute of submit button.
class
attribute of submit button.
'<h3 id="reply-title" class="comment-reply-title">'
'</h3>'
'<small>'
'</small>'
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"';
$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 ),
);
void
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);
comment_form()
is located in wp-includes/comment-template.php
.