I would like to modify the actual HTML of the comment forms in WordPress. I know of the comment_form()
function that WordPress provides, but it doesn't actually let me modify the html of the form. What I'm specifically trying to do is make a bootstrap responsive form, which is not possible with the options provided with the comment_form()
function. I've tried looking all over the internet for a way, but to no avail. Can anyone point me in the right direction?
Let me clarify this:
I am looking to modify the actual FORM and containing DIV elements of the comment form, not just the fields.
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.
Just Create a New PHP file with the name of comment_form.php then paste all of your code. And your comment form is ready.
you shouldn't edit the wp-includes/comment-template.php file.
Keep this for your ref for more styling your existing comment form
http://codex.wordpress.org/Template_Tags/comment_form
Or this one
http://wordpress.org/support/topic/where-to-find-the-comment_form-function-in-wp3
Form code and action
<div class="comment-form">
<h2 class="comments-wrapper-heading"> Leave a comment </h2>
<form id="commentform" action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<div class="commentform-element">
<label class="hide" for="author">Full Name</label>
<input class="input-fields" id="author" name="author" type="text" placeholder="Full Name" value=""/>
</div>
<div class="commentform-element">
<label class="hide" for="author">Email</label>
<input class="input-fields" id="email" name="email" type="text" placeholder="Email" value=""/>
</div>
<div class="commentform-element">
<label class="hide" for="comment">Message</label>
<textarea id="comment" class="input-fields" placeholder="Message" name="comment" cols="40" rows="200"></textarea>
</div>
<input name="submit" class="form-submit-button" type="submit" id="submit-comment" value="Post comment">
<input type="hidden" name="comment_post_ID" value="22" id="comment_post_ID">
<input type="hidden" name="comment_parent" id="comment_parent" value="0">
</form>
</div>
WP does not offer a way to create and use a comment form template. i.e.: as of 4.1.1, there's still no way to control all of the HTML markup that makes up the form. There are only ways to edit parts of it and that's by supplying arguments to comment_form()
.
So, if you would like to edit the whole layout (e.g.: place some of the content side to side instead of on top of one another; replace some of the <p>
with <div>
), you're out of luck.
What can we do?
An option would be to borrow the whole comment_form()
function from the WordPress codebase (like this guy did): make a copy of the function in your theme, rename it, customize it and then call it in place of the vanilla function. That will work but there's a chance it might break on a future WordPress update if some of the inner workings of WP change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With