custom wordpress comment form html

The uncustomizable parts of the function comment_form() should (idealistically) be limited purely to code that is essential for security and proper form handling by the WordPress core. However, that is not the case. To explore the issue, I used filters (could have used arguments) to completely remove all the code that is customizable. I set all available variables to empty strings—that is, all the stuff mentioned on the codex page for comment_form().

Here is the left-over, non-customisable code from comment_form():

<div id="respond" class="comment-respond">
    <h3 id="reply-title" class="comment-reply-title"> <small><a rel="nofollow" id="cancel-comment-reply-link" href="/1#respond" style="display:none;">Click here to cancel reply.</a></small></h3>
    <form action="http://www.[yourdomain].com/wp-comments-post.php" method="post" id="" class="comment-form">
        <p class="form-submit">
            <input name="submit" type="submit" id="" value="" />
            <input type='hidden' name='comment_post_ID' value='1' id='comment_post_ID' />
            <input type='hidden' name='comment_parent' id='comment_parent' value='0' />
        </p>
    </form>
</div><!-- #respond -->

Where does all this come from? At the bottom of the codex page for comment_form() it says…

Source Code

comment_form() is located in wp-includes/comment-template.php.

comment-template.php is linked and you can view the full code in the browser. comment_form() starts on line 1960. In fact, it’s the last function in that file. There are no arguments or filters that let you modify the residual code above. These lines of code are all moreorless “hard-coded”.

The text Click here to cancel reply. is the only text that survived my filter genocide. Strangely, comment_form() has back-up text for cancel_reply_link hard-coded into the function, in case it is passed to the function as an empty string. None of the other filterable items have hard-coded back-ups.

It is easy to see which bits of code are essential and which bits are non-essential for a HTML form. A little more difficult to judge is which bits are essential for a WordPress form. Some of the bits above were dynamically output (note that this is the first comment on a development blog with no other replies, no parent/child comments).

From the comment_form() function in comment-template.php you can draw out the code needed to produce the dynamic parts of the WordPress form. Then, with default arguments taken from the codex page for comment_form(), you could piece together a barebones form, hard-coding the desired fields, labels and wrapping HTML. I’m doing that now and putting my custom form function in my theme’s comments.php template file, which I call using comments_template() only in single.php (for this particular theme).

The result would be a full and proper, lean and mean WordPress comment form. But… it would be a form that could not be customized anymore using comment_form() arguments or related filters unless you went ahead and included the filter code in your own comment form function. Since you’re customizing the heck out it already, that’s probably not a problem. Similarly, all the WordPress actions would also be unavailable to you or any other plugins unless you also triggered those action functions in your own comment form function. Again, probably not an issue for you at this point.

But most importantly, the resulting form might break your theme if future WordPress updates change the way the core handles forms.

If you’re aware of those risks, you can probably rebuild a hand-coded comment form just from copying code on the codex page for comment_form() and in wp-includes/comment-template.php. I don’t have the finished code, otherwise I’d post it. But I will post when/if I succeed.

Right, that’s all from me for now. Bear in mind (all readers) that despite appearances, I am an amateur WordPress theme developer and my proficiency with PHP and WordPress is very rudimentary. Just writing up this post I learned a lot.

I’m also worried that a full and proper solution is already out there somewhere but I haven’t found it in my searches.

Tags:

Php

Wordpress