I searched a lot of thread regarding my problem, but unfortunately I found nothing works, and this my final option. I want to add some custom fields on my comment form. How can I do that?


Here you go: Adding Custom Fields to WordPress Comment Forms?

And another awesome post on this: http://wpengineer.com/2214/adding-input-fields-to-the-comment-form/

Functions are available to add/update, delete comment meta, similar to post and user meta.

Edit: Here's an example to give you a start (put the code into the functions.php or in a custom plugin):

Add the fields to comment form:

add_filter( 'comment_form_defaults', 'change_comment_form_defaults');
function change_comment_form_defaults( $default ) {
    $commenter = wp_get_current_commenter();
    $default[ 'fields' ][ 'email' ] .= '<p class="comment-form-author">' .
                                            '<label for="city">'. __('City') . '</label>
                                        <span class="required">*</span>
                                        <input id="city" name="city" size="30" type="text" /></p>';
    return $default;

4 functions to retrieve/add/update/delete comment meta:

get_comment_meta( $comment_id, $meta_key, $single = false );
add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false );
update_comment_meta($comment_id, $meta_key, $meta_value, $unique = false );
delete_comment_meta( $comment_id, $meta_key, $single = false );

This is where you'd do the validations:

add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
    if ( ! isset( $_POST['city'] ) )
        wp_die( __( 'Error: please fill the required field (city).' ) );
    return $commentdata;

And save the comment meta:

add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
    add_comment_meta( $comment_id, 'city', $_POST[ 'city' ] );

Retrieve and display comment meta:

add_filter( 'get_comment_author_link', 'attach_city_to_author' );
function attach_city_to_author( $author ) {
    $city = get_comment_meta( get_comment_ID(), 'city', true );
    if ( $city )
        $author .= " ($city)";
    return $author;

(Note: All the code is from the WPengineer link I posted above. There are more details and advanced usages in that post, please check them too! )


This slideshow from Beau Lebens should be able to show you how: Hooking into Comments

And this blog post from Otto should be able to show you more: WordPress 3.0 Theme Tip: The Comment Form

There is also a basic plugin available here called "Wordpress Plugin: Extra Comment Fields" (sorry can't post the link).

  • 2
    Links can get deleted. Please improve your answer with some of the content you found on the links. Best would be to add an example. – kaiser Jun 28 '12 at 7:31
  • thanks for the answer buddy, it helps me too but id rather choose rutwick's answer. Thank you :) – markyeoj Jun 28 '12 at 7:41

protected by fuxia Apr 11 '13 at 19:32

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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