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 want to add a field to the comments and have used these codes.

functions.php

function my_fields($fields) {
    $fields['url2'] = '<p class="comment-form-url2">
                          <label for="url2">URL hittad på webben</label>
                          <input id="url2" name="url2" type="text" value="" size="30" />
                       </p>';
    return $fields;
}
add_filter('comment_form_default_fields','my_fields');

comments.php

comment_form();

Questions

  1. It does not add an extra field in admin comment. Should it, or not?
  2. It does not seem to save the extra field. I tried to var dump the comment array. Why not?
share|improve this question

3 Answers 3

up vote 6 down vote accepted

There are a couple other hooks in the comment form that you can use. Where you're hooking on only displays if the user isn't logged in. If you want that field for all users (logged in or not), you need to add your form by hooking into both comment_form_after_fields and comment_form_logged_in_after, both of which are actions, and echo out the new field.

<?php
add_action( 'comment_form_logged_in_after', 'pmg_comment_tut_fields' );
add_action( 'comment_form_after_fields', 'pmg_comment_tut_fields' );
function pmg_comment_tut_fields()
{
    ?>
    <p class="comment-form-title">
        <label for="pmg_comment_title"><?php _e( 'Title' ); ?></label>
        <input type="text" name="pmg_comment_title" id="pmg_comment_title" />
    </p>
    <?php
}

Check out this tutorial I wrote (the example above is from it). Covers everything from adding fields to saving the data to adding a meta box so you can edit the extra fields on the back end too.

share|improve this answer
    
Can I ask what jquery lightbox plugin you're using on your site? –  AlxVallejo Jul 12 '12 at 19:06
    
Which site, the one linked above or another? It's probably Lightbox 3, but I can't find that one in the WP.org repo any more. –  chrisguitarguy Jul 12 '12 at 19:59
    
Your tutorial. Also, regarding the tutorial, you reference the comment title as $title in Part 2. How does the title from part 1 become $title? –  AlxVallejo Jul 12 '12 at 20:16
    
Oh I see, nevermind. –  AlxVallejo Jul 12 '12 at 20:24

To save your extra field, you have to do :

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

See this nice tutorial covering extra fields in comment forms.

share|improve this answer

Take a look at this plugin http://www.solaceten.info

(disclosure: I'm the co-author. The plugin is free, but not maintained any longer)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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