comment_form() – Outputs a complete commenting form for use within a template.

You appear to be a bot. Output may be restricted

Description

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 comment_form_field_$name where $name is the key used in the array of fields.

Usage

comment_form( $args, $post );

Parameters

$args
( array ) optional – { Optional. Default arguments and form fields to override.
$fields
( array ) optional – { Default comment fields, filterable by default via the comment_form_default_fields hook.
$author
( string ) optional – Comment author field HTML.
$email
( string ) optional – Comment author email field HTML.
$url
( string ) optional – Comment author URL field HTML.
$cookies
( string ) optional – Comment cookie opt-in field HTML. }
$comment_field
( string ) optional – The comment textarea field HTML.
$must_log_in
( string ) optional – HTML element for a 'must be logged in to comment' message.
$logged_in_as
( string ) optional – The HTML for the 'logged in as [user]' message, the Edit profile link, and the Log out link.
$comment_notes_before
( string ) optional – HTML element for a message displayed before the comment fields if the user is not logged in. Default 'Your email address will not be published.'.
$comment_notes_after
( string ) optional – HTML element for a message displayed after the textarea field.
$action
( string ) optional – The comment form element action attribute. Default '/wp-comments-post.php'.
$id_form
( string ) optional – The comment form element id attribute. Default 'commentform'.
$id_submit
( string ) optional – The comment submit element id attribute. Default 'submit'.
$class_container
( string ) optional – The comment form container class attribute. Default 'comment-respond'.
$class_form
( string ) optional – The comment form element class attribute. Default 'comment-form'.
$class_submit
( string ) optional – The comment submit element class attribute. Default 'submit'.
$name_submit
( string ) optional – The comment submit element name attribute. Default 'submit'.
$title_reply
( string ) optional – The translatable 'reply' button label. Default 'Leave a Reply'.
$title_reply_to
( string ) optional – The translatable 'reply-to' button label. Default 'Leave a Reply to %s', where %s is the author of the comment being replied to.
$title_reply_before
( string ) optional – HTML displayed before the comment form title. Default: '<h3 id="reply-title" class="comment-reply-title">'.
$title_reply_after
( string ) optional – HTML displayed after the comment form title. Default: '</h3>'.
$cancel_reply_before
( string ) optional – HTML displayed before the cancel reply link.
$cancel_reply_after
( string ) optional – HTML displayed after the cancel reply link.
$cancel_reply_link
( string ) optional – The translatable 'cancel reply' button label. Default 'Cancel reply'.
$label_submit
( string ) optional – The translatable 'submit' button label. Default 'Post a comment'.
$submit_button
( string ) optional – HTML format for the Submit button. Default: '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />'.
$submit_field
( string ) optional – HTML format for the markup surrounding the Submit button and comment hidden fields. Default: '<p class="form-submit">%1$s %2$s</p>', where %1$s is the submit button markup and %2$s is the comment hidden fields.
$format
( string ) optional – The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'. }
$post
( int|WP_Post ) optional – Post ID or WP_Post object to generate the form for. Default current post.

Returns

void

Source

File name: wordpress/wp-includes/comment-template.php


Lines:

1 to 100 of 437
function comment_form( $args = array(), $post = null ) {
  $post = get_post( $post );

  // Exit the function if the post is invalid or comments are closed.
  if ( ! $post || ! comments_open( $post ) ) {
    
/**
 * Fires after the comment form if comments are closed.
 *
 * For backward compatibility, this action also fires if comment_form()
 * is called with an invalid post object or ID.
 *
 * @since 3.0.0
 */
    do_action( 'comment_form_comments_closed' );

    return;
  }

  $post_id       = $post->ID;
  $commenter     = wp_get_current_commenter();
  $user          = wp_get_current_user();
  $user_identity = $user->exists() ? $user->display_name : '';

  $args = wp_parse_args( $args );
  if ( ! isset( $args['format'] ) ) {
    $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
  }

  $req   = get_option( 'require_name_email' );
  $html5 = 'html5' === $args['format'];

  // Define attributes in HTML5 or XHTML syntax.
  $required_attribute = ( $html5 ? ' required' : ' required="required"' );
  $checked_attribute  = ( $html5 ? ' checked' : ' checked="checked"' );

  // Identify required fields visually and create a message about the indicator.
  $required_indicator = ' ' . wp_required_field_indicator();
  $required_text      = ' ' . wp_required_field_message();

  $fields = array(
    'author' => sprintf(
      '<p class="comment-form-author">%s %s</p>',
      sprintf(
        '<label for="author">%s%s</label>',
        __( 'Name' ),
        ( $req ? $required_indicator : '' )
      ),
      sprintf(
        '<input id="author" name="author" type="text" value="%s" size="30" maxlength="245" autocomplete="name"%s />',
        esc_attr( $commenter['comment_author'] ),
        ( $req ? $required_attribute : '' )
      )
    ),
    'email'  => sprintf(
      '<p class="comment-form-email">%s %s</p>',
      sprintf(
        '<label for="email">%s%s</label>',
        __( 'Email' ),
        ( $req ? $required_indicator : '' )
      ),
      sprintf(
        '<input id="email" name="email" %s value="%s" size="30" maxlength="100" aria-describedby="email-notes" autocomplete="email"%s />',
        ( $html5 ? 'type="email"' : 'type="text"' ),
        esc_attr( $commenter['comment_author_email'] ),
        ( $req ? $required_attribute : '' )
      )
    ),
    'url'    => sprintf(
      '<p class="comment-form-url">%s %s</p>',
      sprintf(
        '<label for="url">%s</label>',
        __( 'Website' )
      ),
      sprintf(
        '<input id="url" name="url" %s value="%s" size="30" maxlength="200" autocomplete="url" />',
        ( $html5 ? 'type="url"' : 'type="text"' ),
        esc_attr( $commenter['comment_author_url'] )
      )
    ),
  );

  if ( has_action( 'set_comment_cookies', 'wp_set_comment_cookies' ) && get_option( 'show_comments_cookies_opt_in' ) ) {
    $consent = empty( $commenter['comment_author_email'] ) ? '' : $checked_attribute;

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

    // Ensure that the passed fields include cookies consent.
    if ( isset( $args['fields'] ) && ! isset( $args['fields']['cookies'] ) ) {
      $args['fields']['cookies'] = $fields['cookies'];

 View on GitHub View on Trac