Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!
Function Reference/comment form
Contents
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(); ?>
As seen in the popular twentyten theme - called here:
wp-content/themes/twentyten/comments.php
Parameters
- 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'.
- Default:
apply_filters( 'comment_form_default_fields', $fields )
- Default:
- 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>'
- Default:
- 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>'
- Default:
- 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>'
- Default:
- 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>'
- Default:
- 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>'
- Default:
- id_form
- (string) (optional) value of the
id
attribute ofform
element (<form>
tag).- Default: 'commentform'
- 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' )
$fields
Default form fields:
$fields = array( 'author' => '<p class="comment-form-author"><label for="author">' . __( 'Name', 'domainreference' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<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' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<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>', );
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'" : '' );
Default $args array
$args = array(
'id_form' => 'commentform',
'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
- comment_form_default_fields
- the_permalink
- comment_form_defaults
- comment_form_logged_in
- comment_form_field_{$name}
- comment_form_field_comment
Pluggable actions
- comment_form_before
- comment_form_must_log_in_after
- comment_form_top
- comment_form_logged_in_after
- comment_form_before_fields
- comment_form_after_fields
- comment_form
- comment_form_after
- comment_form_comments_closed
Changelog
- Since 3.0.0
Source Code
comment_form()
is located in wp-includes/comment-template.php
.Related
Comments Functions
- Function: cancel_comment_reply_link()
- Function: comment_author()
- Function: comment_author_email()
- Function: comment_author_email_link()
- Function: comment_author_IP()
- Function: comment_author_link()
- Function: comment_author_rss()
- Function: comment_author_url()
- Function: comment_author_url_link()
- Function: comment_class()
- Function: comment_date()
- Function: comment_excerpt()
- Function: comment_form_title()
- Function: comment_form(),
- Function: comment_ID()
- Function: comment_id_fields()
- Function: comment_reply_link()
- Function: comment_text()
- Function: comment_text_rss()
- Function: comment_time()
- Function: comment_type()
- Function: comments_link
- Function: comments_number()
- Function: comments_open(),
- Function: comments_popup_link()
- Function: comments_popup_script()
- Function: comments_rss_link()
- Function: get_avatar()
- Function: next_comments_link()
- Function: paginate_comments_links()
- Function: permalink_comments_rss()
- Function: previous_comments_link()
- Function: wp_list_comments()
See also index of Function Reference and index of Template Tags.
No comments:
Post a Comment