Home WordPress Comment Functions comment_form() Definition In WordPress, comment_form() prints the complete comment form . It includes the name, email, website, and comment fields, a submit button, and hidden fields.
It prints the form. Put it at the end of comments.php, after the list of comments. You can change the text and the fields with arguments.
Syntax Call it with no arguments for the default form, or pass an array to change it.
comment_form();
comment_form( array( 'title_reply' => 'Join the talk' ) );
comment_form( $args, $post_id ); Call What it does comment_form()Prints the default comment form. comment_form( array( 'title_reply' => 'Join the talk' ) )Prints the form with a new heading. comment_form( $args, 42 )Prints the form for the post with ID 42.
Common keys in $args are title_reply, label_submit, comment_field, fields, and class_submit.
Full declaration comment_form( array $args = array(), int|WP_Post|null $post = null ): void Parameters Parameter Description $argsOptional. An array that changes the form. Keys include title_reply, label_submit, comment_field, fields, class_form, class_submit, and comment_notes_before. $postOptional. A post ID or WP_Post object. Default is the current post.
Return Value Return Value Description void Nothing is returned. The function prints the whole form.
WordPress Examples Three short theme examples. Each one has a sample output.
1. Default form 2. Change the text 3. Remove the website field Example 1 — Default form Print the standard form at the end of comments.php.
<?php
if ( comments_open() ) {
comment_form();
}
?> <div id="respond" class="comment-respond">
<h3 id="reply-title" class="comment-reply-title">Leave a Reply</h3>
<form action="/wp-comments-post.php" method="post" id="commentform" class="comment-form">
...
<input name="submit" type="submit" id="submit" class="submit" value="Post Comment" />
</form>
</div> How It Works comments_open() checks that comments are allowed.comment_form() prints the form with default text.The form posts to wp-comments-post.php. Example 2 — Change the text Change the heading and the button text.
<?php
comment_form( array(
'title_reply' => 'Join the discussion',
'label_submit' => 'Send comment',
) );
?> <h3 id="reply-title" class="comment-reply-title">Join the discussion</h3>
...
<input name="submit" type="submit" id="submit" class="submit" value="Send comment" /> How It Works title_reply sets the heading.label_submit sets the text on the button.All other parts keep their default values. Example 3 — Remove the website field Use a filter to remove the website field from the form.
add_filter( 'comment_form_default_fields', function ( $fields ) {
unset( $fields['url'] );
return $fields;
} ); <p class="comment-form-author">...</p>
<p class="comment-form-email">...</p>
<!-- no website field --> How It Works The filter gives you the array of default fields. unset( $fields['url'] ) removes the website field.Put the code in functions.php. comment_form() uses the new list. Frequently Asked Questions What does comment_form() do? It prints the full comment form, including the fields, the submit button, and the hidden fields.
Where do I put comment_form()? At the end of comments.php, after the list of comments.
Why is the form not showing? Comments may be closed for the post, or your theme does not load comments.php.
How do I change the button text? Pass 'label_submit' in the $args array.
Do I need comment_id_fields() with it? No. comment_form() prints the hidden fields for you.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion