How to style and change comment form in WordPress

The WordPress function comment_form() is used for generating commenting form within WordPress theme. The great thing about this function is that it has wide range of parameters you can use to perform fine tuning. You could do something as simple as changing the wording (“Leave a reply”), or you could also create advanced layouts. For example, why would you have author and email input fields both in single line, given that in wider layouts on higher resolutions commenter will not fill even 50% of the input field?

As for available parameters, if you check official WordPress documentation, you will find the following:

  • ‘fields’ (array) { Default comment fields, filterable by default via the ‘comment_form_default_fields’ hook.
  • ‘author’ (string) Comment author field HTML.
  • ’email’ (string) Comment author email field HTML.
  • ‘url’ (string) Comment author URL field HTML. }
  • …and many others.

Most of these can be changed only by adding new items into functions’ array, such as (make sure you include text domain for translation purposes):

But if you look at the list of parameters closely, you’ll find ‘fields’ => $fields. You may omit this one at first, but as it turns out, this could be the most important parameter for any advanced layout change. Let’s check on how it looks like and WordPress core:

By the quick look, $fields array is what does the magic – if determines how the layout of default form will look like. Also, in order to avoid PHP warnings and notices (undeclared variable), you will also need to redeclare $req, $aria_req, etc.

Finally, this is how it would look like implemented into comments.php template file:

BUT, if you were careless as I was, you likely omitted the fact that $fields is filterable via ‘comment_form_default_fields’ hook. If unsure how to use filters, check on my quick tutorial.

Implementation through filters is easy and would look like this:

While our comment_form function call would look like this:

Both approaches seem to be working, but given that WordPress core developers added filter here, conclusion is simple – use filter!