Using WordPress ‘comment_form()’ PHP function

The comment_form() WordPress PHP function outputs a complete commenting form for use within a template. You can control most strings and form fields via the $args array passed into the function. If you’d like to add a new field or remove a single field, you can use the comment_form_default_fields filter to modify the default fields array. All fields are individually passed through a filter of the comment_form_field_$name, where $name is the key used in the array of fields.

Usage

Here’s an example of using the comment_form() function:

$comments_args = array(
  'title_reply' => 'Share Your Thoughts',
  'label_submit' => 'Post Comment'
);

comment_form($comments_args);

This will generate a comment form with the title “Share Your Thoughts” and the submit button labeled “Post Comment”.

Parameters

  • $args (array): Optional. Default arguments and form fields to override. This can include things like the form id attribute, class attribute, submit button label, and more. Default is an empty array.
  • $post_id (int|WP_Post): Optional. The post ID or WP_Post object to generate the form for. Default is the current post.

More Information

See WordPress Developer Resources: comment_form()

Examples

Change some comment form fields

$comments_args = array(
  'label_submit' => 'Send',
  'title_reply' => 'Write a Reply or Comment',
  'comment_notes_after' => '',
  '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);

This example changes the label of the submit button, the title of the reply section, removes the text or HTML displayed after the set of comment fields, and redefines the comment body textarea.

Change the order of comment fields

add_filter('comment_form_fields', 'mo_comment_fields_custom_order');
function mo_comment_fields_custom_order($fields) {
  $comment_field = $fields['comment'];
  $author_field = $fields['author'];
  $email_field = $fields['email'];
  $url_field = $fields['url'];
  $cookies_field = $fields['cookies'];

  unset($fields['comment']);
  unset($fields['author']);
  unset($fields['email']);
  unset($fields['url']);
  unset($fields['cookies']);

  $fields['author'] = $author_field;
  $fields['email'] = $email_field;
  $fields['url'] = $url_field;
  $fields['comment'] = $comment_field;
  $fields['cookies'] = $cookies_field;

  return $fields;
}

This example modifies the order of comment fields.

Translate Friendly options

$comments_args = array(
  'fields' => array(
    'author' => '<p class="comment-form-author"><br /><input id="author" name="author" aria-required="true" placeholder="' . $comment_author .'"></input></p>',
    'email' => '<p class="comment-form-email"><br /><input id="email" name="email" placeholder="' . $comment_email .'"></input></p>',
    'url' => '<p class="comment-form-url"><br /><input id="url" name="url" placeholder="' . $comment_url .'"></
input></p>',
    'cookies' => '<input type="checkbox" required>' . $comment_cookies_1 . '<a href="' . get_privacy_policy_url() . '">' . $comment_cookies_2 . '</a>',
  ),
  'label_submit' => __( $comment_send ),
  'title_reply' => __( $comment_reply ),
  'title_reply_to' => __( $comment_reply_to ),
  'cancel_reply_link' => __( $comment_cancel ),
  'comment_field' => '<p class="comment-form-comment"><br /><textarea id="comment" name="comment" aria-required="true" placeholder="' . $comment_body .'"></textarea></p>',
  'comment_notes_before' => __( $comment_before),
  'comment_notes_after' => '',
  'id_submit' => __( 'comment-submit' ),
);

comment_form($comments_args);

This example provides a list of translate-friendly options, declaring variables for each component of the form and using these variables in the $comments_args array.

Change the form format to HTML5

$comments_args = array(
  'format' => 'html5',
);

comment_form($comments_args);

This example changes the comment form format to HTML5.

Use post ID to generate form

$post_id = 123; // Assuming 123 is a valid post ID
$comments_args = array();

comment_form($comments_args, $post_id);

This example demonstrates how to generate a comment form for a specific post, identified by post ID.