• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / 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_id );

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 – HTML element for a 'logged in as [user]' message.
$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_id
( 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 423
function comment_form( $args = array(), $post_id = null ) {
  if ( null === $post_id ) {
    $post_id = get_the_ID();
  }

  // Exit the function when comments for the post are closed.
  if ( ! comments_open( $post_id ) ) {
    
/**
 * Fires after the comment form if comments are closed.
 *
 * @since 3.0.0
 */
    do_action( 'comment_form_comments_closed' );

    return;
  }

  $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' );
  $html_req = ( $req ? " required='required'" : '' );
  $html5    = 'html5' === $args['format'];

  $fields = array(
    'author' => sprintf(
      '<p class="comment-form-author">%s %s</p>',
      sprintf(
        '<label for="author">%s%s</label>',
        __( 'Name' ),
        ( $req ? ' <span class="required">*</span>' : '' )
      ),
      sprintf(
        '<input id="author" name="author" type="text" value="%s" size="30" maxlength="245"%s />',
        esc_attr( $commenter['comment_author'] ),
        $html_req
      )
    ),
    'email'  => sprintf(
      '<p class="comment-form-email">%s %s</p>',
      sprintf(
        '<label for="email">%s%s</label>',
        __( 'Email' ),
        ( $req ? ' <span class="required">*</span>' : '' )
      ),
      sprintf(
        '<input id="email" name="email" %s value="%s" size="30" maxlength="100" aria-describedby="email-notes"%s />',
        ( $html5 ? 'type="email"' : 'type="text"' ),
        esc_attr( $commenter['comment_author_email'] ),
        $html_req
      )
    ),
    '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" />',
        ( $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="checked"';

    $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'];
    }
  }

  $required_text = sprintf(
    /* translators: %s: Asterisk symbol (*). */
    ' ' . __( 'Required fields are marked %s' ),
    '<span class="required">*</span>'
  );

  
[1] [2] [3] … [5] Next »

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 9th December 2020

Primary Sidebar

Information

Function name: comment_form
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-includes/comment-template.php
File ref: wp-includes/comment-template.php
Deprecated?: No
API Letters: C,F

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites