3

Is it possible to change the fields that are available in the comments form? By default my comments form is asking for Name, Email, Website, and then the actual message. I'd like to remove website and replace it with a new field of my choosing.

What's the best way to accomplish this?

2 Answers 2

5

This answer can change depending on the theme or plugins you have installed, but the code can usually be found in similar places of the theme editor.

For example, if you are using the default Twenty Eleven theme in WordPress 3.2.1, you can verify the location of the comments call by checking here:

  1. Login to your admin interface.
  2. Click on the Appearance menu.
  3. Click on Editor under Appearance.
  4. Click Comments on the right side to edit the comments.php file.

This will bring up the page that WordPress uses to generate the comments display.

Some themes may have entered code that you can edit directly to change the display. Twenty Eleven uses the default WordPress function 'comment_form()' to display the form used for new comments.

A lot can be done to change the values at this point, and you should take a look at the full documentation of comment_form() for all of the details. To get you started though, click on Theme Funcitons on the right side to open the functions.php file and add the following at the bottom:

// This creates a filter to change the default comment fields.
add_filter('comment_form_default_fields', 'my_comment_changes'); 

function my_comment_changes ( $my_fields ) {
    $my_fields[ 'url' ] = ''; //This will make the website area display nothing.
    $my_fields[ 'custom' ] = 'test field only<br>'; // This will add a custom field.

    return $my_fields;
}

It can definitely get more complex than that if you need it to, but this will get you started.

5
  • can you not also unset( $my_fields['url'] ); ?
    – Milo
    Commented Oct 7, 2011 at 3:21
  • Yup, unset( $my_fields[ 'url' ] ); would blank it out as well as $my_fields[ 'url '] = NULL; Commented Oct 7, 2011 at 3:26
  • This code unfortunately did nothing for me. I am using a custom theme though so I may have to figure something else out I guess.
    – jkupczak
    Commented Oct 8, 2011 at 19:33
  • Can you clarify what you mean by "this code did nothing"? If your custom Theme is using comment_form(), then this code is the correct approach. If your custom Theme isn't using comment_form(), then it should be. Commented Jan 18, 2012 at 16:41
  • Changes like that should probably put into tiny plugins so that you are stable against core upgrade.
    – Raphael
    Commented Jan 18, 2012 at 18:33
0

Way to add a new field...

add_filter('comment_form_default_fields', 'new_fields');

function new_fields($fields)
{
    $fields['my_field'] = "<input type='text' name='my_field' />";
    return $fields;
}

Apologies, but I don't have an idea how to save the new field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.