Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

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;
}

3 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! )

share|improve this answer
add comment

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).

share|improve this answer
1  
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
add comment

protected by toscho Apr 11 '13 at 19:32

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

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.