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 forcomment_form()
.Here is the left-over, non-customisable code from
comment_form()
:Where does all this come from? At the bottom of the codex page for
comment_form()
it saysâ¦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 forcancel_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 incomment-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 forcomment_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âscomments.php
template file, which I call usingcomments_template()
only insingle.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
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.Did you look for the comment_form() function in WordPress code to see if you can edit it ?
You’ll also find some informations here :
http://wordpress.org/support/topic/editing-the-comment-form
This function uses the default form. You need to make a
comment-template.php
file in your theme and in that file you can make your form.Now when you will call
comments_template()
you will get your made form instead of default form.I don’t think you can change the containing div (
<div id="respond" class="comment-respond">
). Have a look at the comment-template.phpThe only thing you can change is the form ID.
'id_form' => 'commentform'
You can make a responsive form without changing these elements.
I’ve recently answered something similiar on WPSE. You can go and check it out here