0

I have added more data to my comment using hooks. I added a group of radio buttons so users can select and choose one of radio buttons. Everything works perfectly fine but the newly added radio buttons do not show up when you're already logged in.. they only show up and work when you are not logged in. Logged in users only see the comment box.

How to show the newly added field alongside with the comment box to logged in user?

this is what I added to my functions.php

function add_comment_fields($fields) {

    if(is_user_logged_in()) {

        $fields['age'] = '<p class="comment-form-age"><label for="age">' . __( 'Age' ) . '</label>' .
        '<input id="age" name="age" type="text" size="30"' . $aria_req . ' /></p>';
        return $fields;
    }
}
add_filter('comment_form_default_fields','add_comment_fields');


function add_comment_meta_values($comment_id) {

    if(isset($_POST['age'])) {
        $age = wp_filter_nohtml_kses($_POST['age']);
        add_comment_meta($comment_id, 'age', $age, false);
    }

}
add_action ('comment_post', 'add_comment_meta_values', 1);
3
  • Try to explain the same in terms of code. You are free to edit the Question whenever needed.
    – brasofilo
    Feb 14, 2013 at 7:24
  • this really depends on your theme and comment form template
    – Bainternet
    Feb 14, 2013 at 8:20
  • I am working with twenty twelve theme
    – hnnnng
    Feb 14, 2013 at 20:40

1 Answer 1

0

One idea could be to save all your additional data on as user meta, which would allow you to retrieve it when a comment is posted by a logged in user and store a copy of it along side the comment as you would for non-logged in users. Naturally this depends on the kind of data, and the extent to which it has to be unique for each comment.

An alternative approach might be to hook into the comment_form_top action, which is called by comment_template() before printing the comment form. Assuming that you have added the additional fields using the comment_form_default_fields filter, you would then just do something like this:

function get_extra_fields() {
    return array('age' => '<p class="comment-form-age"><label for="age">' . __( 'Age' ) . '</label>' .
        '<input id="age" name="age" type="text" size="30"' . '' . ' /></p>');
}

function add_comment_fields($fields) {

    $fields = array_merge($fields, get_extra_fields());

    return $fields;
}
add_filter('comment_form_default_fields','add_comment_fields');


function add_comment_meta_values($comment_id) {

    if(isset($_POST['age'])) {
        $age = wp_filter_nohtml_kses($_POST['age']);
        add_comment_meta($comment_id, 'age', $age, false);
    }

}
add_action ('comment_post', 'add_comment_meta_values', 1);


function add_comment_fields_logged_in() {
    if (!is_user_logged_in())
        return;

    $fields = get_extra_fields();

    foreach ( $fields as $name => $field ) {
        echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
    }
}
add_filter('comment_form_logged_in_after', 'add_comment_fields_logged_in');

Possibly you will also need to do some checking to verify that all required data has been submitted by the form etc., but that will likely apply for non-logged in users as well.

3
  • Simon, WordPress by default, shows only the comment box to logged in user, because it already has collected the user's name and email address. I want to show this additional field when the user is logged in alongside with the comment box. I updated my question, please see the code I am using. Thanks!
    – hnnnng
    Feb 14, 2013 at 20:35
  • I might have had an error in my previous code example, but basically that should have worked (as long as I understand you question correctly, but I think I do). I've updated my answer and exchanged comment_form_top to comment_form_logged_in_after, and broken out the $fields setup to a separate function (get_extra_fields) which can be used both for logged in and non-logged in users. The supplied code adds the Age field above to comment box in both cases.
    – Simon
    Feb 15, 2013 at 19:39
  • 1
    Simon, it worked like a charm!.. Thank you so much!
    – hnnnng
    Feb 16, 2013 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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