Recently a client wanted to change the comment form on a WordPress site I was building for them. Specifically, they just wanted to have a Name field and the Comment field – omitting the E-mail and Website fields. They also wanted their labels to have particular wording.
I had never done this before, and after a lot of googling, I figured it out. I didn’t feel that a lot of the tutorials out there provided enough information, so I’m hoping to rectify that problem with this post.
I’m going to assume that you are a developer type who is setting up a WordPress site. These techniques work on WordPress version 3 and up. In my examples, though, I’ll be writing to the current release at the time of this post – 3.3.1. I am going to assume that you are familiar with how a theme is constructed, and that you aren’t scared by a little PHP. It’s really not that bad.
What I’m not going to cover
I’m not going to go into how to add pieces to the comment form that aren’t already there – e.g. adding another field or button. That involves intercepting the form and making sure the database can handle it – and frankly is above my head at the moment.
These techniques all involve files that are found in your theme. While you could hack WordPress core, that is highly inadvisable. Every time you updated your WordPress install, you’d have to redo your changes.
I’m going to discuss two techniques, and each affects a particular file. The first technique involves comments.php, which is found in most themes. A theme doesn’t necessarily have a comments.php file, but it is generally considered best practice to do so.
The second technique involves changes to functions.php, which again is found in most themes. Any real logic that happens in your theme tends to be found in a theme’s functions.php.
Brief historical interlude
The comment form in WordPress used to be a fairly straightforward affair, but a messy one. Previous to version 3, the entire comment form, markup and everything, lived in a theme’s comments.php file. While this made it a snap to alter the comment form, it resulted in a big hairy ball of code living in a theme.
In WordPress 3, there was an effort to abstract this functionality, resulting in one simple call that would be needed in your theme:
<?php comment_form(); ?>
This one line will produce the comment form that we all know and love. Which is, ultimately, a good thing. 96.429% of WordPress sites out there (I asked) use the default comment form. There is no reason to have that big mess living in the theme of the vast majority of sites.
However, if you do want to alter the comment form, it’s now a little more tricky than simply locating the markup you want to change.
A tale of two methods
There are two ways to go about altering your comment form, as I’ve mentioned. Which method you choose depends on what you want to accomplish.
Please note: Method 2 will trump Method 1, so choose wisely
Method 1: Altering the comment_form() call in comments.php
This method is good for
Altering all of the details about the comment form
Changing the markup of the fields, including labels
This method basically entails taking the normal comment form call:
<?php comment_form(); ?>
And expanding it with new details about how you want the form to be generated. If you want to change some of the details, you need to specify those changes with an array that will be the argument for the function. Like so:
What parameters can you set? Pretty much anything to do with the comment form. Take a look at the WordPress Codex entry on comment_form(), which gives you a run down of the parameters that you can set, and their default values (which WordPress will use unless you specify otherwise).
If you need to set more than one parameter, but sure to include a comma at the end of each parameter, except the last one.
If you want to change the fields of the comment form, that takes a little bit more effort. You will need to create a fields array (I’m going to call it $fields) within the array you just created, and that second array will be called by the ‘fields’ parameter. So we will start out with that new array:
This has redefined how I want the “author” field to be generated. In this case, I’ve changed the label text to say “Your Name” and the initial value of the field itself. Now I need to call that field within the parameters:
Tells WordPress you’re going to change the fields of the comment form, then, you just supply in the name of your array ($fields, in this case). You can change each of the fields using this array technique – author, email, website and comment.
This whole comment_form() function now goes in your comments.php file at the point where you want the comment form to appear – which is usually after the listing of existing comments.
Method 2: Using filters in functions.php
This method is good for
Omitting or changing fields
If you want the nuclear option, and never want to hear from those fields again
The second method employs WordPress filters. Using a filter, you can override the comment form before final output.
You will want to add this filter to your theme’s functions.php – which most themes will have. If your theme doesn’t have a functions.php file, simply create one and make sure it opens with
Next you will want to create a function that defines your changes. As far as I’m aware, you can only modify the fields of the comment form with this method. This is an example function:
$fields['email'] = ''; //removes email field
$fields['url'] = ''; //removes website field
The name of the function can be whatever you want – it will be called later. This example function will remove the email and website fields from the comment form. Want to change the author field as well?
Keep in mind that if you want to modify a field, you have to include everything that is part of the field – label and input. In this example, I’m changing the label to say “Your name, please”, and I’m modifying the input to have a placeholder attribute.
Okay, you’ve made the modifications, now you have to call this function. You do that with a filter – in this case, one that WordPress has built in and understands – it’s called “comment_form_default_fields”:
So you take that function and filter call, put it in your functions.php file, and give it a whirl. This won’t affect where on the page your comment form appears, but rather the fields within it. The placement of the form is still controlled by your comments.php file, and where you put the comment_form() call.
Again, bear in mind that the filter method will override whatever you do in comments.php.
As an aside, I like to keep this snippet around so I can drop any of the fields quickly -
//$fields['author'] = ''; //removes name field
//$fields['email'] = ''; //removes email field
//$fields['url'] = ''; //removes website field
Just put that in your functions.php and uncomment the line with the field you want to omit. Boom. Done.
I hope this post will help you make changes to your WordPress comment form. I’ve tried to be comprehensive and provide context, so sorry it’s a little long. If you use these techniques – I’d love to hear about it in the comments.