Change the HTML output of comments

Upon request, a bit more of inormation 🙂 The default html output of
comment_form() is:

<p class="comment-form-author"><label for="author">Name</label> <span class="required">*</span><input id="author" name="author" type="text" value="" size="30" aria-required='true' /></p>
<p class="comment-form-email"><label for="email">Email</label> <span class="required">*</span><input id="email" name="email" type="text" value="" size="30" aria-required='true' /></p>
 <p class="comment-form-url"><label for="url">Website</label><input id="url" name="url" type="text" value="" size="30" /></p> I would like to wrap all three paragraphs inside an html element (ie. DIV).

As a matter of fact, this is quite easy! You just need to know the right hooks to use.

The comment_form() function includes, among several other action hooks, two that will suit your needs perfectly: comment_form_before_fields and comment_form_after_fields. You can use these hooks to add your wrapper <div>:

<?php
function wpse53671_comment_form_before_fields() {
    echo '<div>';
}
add_action( 'comment_form_before_fields', 'wpse53671_comment_form_before_fields' );

function wpse53671_comment_form_after_fields() {
    echo '</div>';
}
add_action( 'comment_form_after_fields', 'wpse53671_comment_form_after_fields' );
?>

(Note: if anything else is hooking into these actions, you may need to adjust the callback priority, so that your tags nest properly.

Leave a Comment