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.

In a plugin that I am writing I need to let user choose an option from a dropdown list before they post a comment. I used the comment_form action and inserted a drop down into the comment form.

/**
 * Modify the comment to add our custom left and right dropdown
 */
function modify_comment_form($post_id) {
    echo "
    <label class='comment'>Choose your side:</label>
    <select id='tallytree_side' name='tallytree_side'>
        <option selected='selected' value='neutral'>Neutral Comment</option>
        <option value='left_answer'>" . $this->get_left_answer() . "</option>
        <option value='right_answer'>" . $this->get_right_answer() . "</option>
    </select>";
}
add_action('comment_form', 'modify_comment_form');

Which looks like this:

enter image description here

Now I just need to be able to save the value of this dropdown list when the comment is posted as a meta data of this particular comment. Note that this is a plugin and I dont want to modify core wordpress files as suggested in a tutorial here. I found a relevant discussions on the wordpress forum but I still could not find the answer I was looking for.

How can I save the selected option value as a comment meta in wordpress database?

Do I need to use AJAX for this or can this be achieved by simply using an action or filter that I could use to retrieve the value of the dropdown menu?

share|improve this question
    
possible duplicate of Add Comment Custom Field –  kaiser Jul 30 '12 at 13:58
    
I dont think it is exact duplicate. But it did help me understand a bit more what I needed to do to process custom fields in the comment form. Thanks! –  Aras Jul 30 '12 at 23:52

1 Answer 1

You can use the function update_user_meta for logged in users and the php function setcookie for the visitors. Here is a start how to work with update_user_meta

You can run this function on submission of the form or by running it by AJAX, i would recommend you to run it with AJAX.

function wpse_update_user_commentform() {

    /**
     * Get values from the selected
     * Dropdown as tha variable $side
     * Change this to your dropdown name
    */

    $side = $_POST['dropdown'];

    $comment_id = get_comment_ID();

    // Update the comment meta "comment_side" to the selected value
    update_comment_meta( $comment_id, 'comment_side', $side );

}
share|improve this answer
    
Why do you want to add user meta data? We got a dedicated comments and comments meta table. –  kaiser Jul 30 '12 at 13:56
    
Thank you for your response. As I asked in the question I wanted to save the data as comment meta. Please edit your answer to save for comment meta instead of user and I will accept it. –  Aras Jul 30 '12 at 23:48

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.