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:
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?
You can use the function
update_user_meta
for logged in users and the php functionsetcookie
for the visitors. Here is a start how to work withupdate_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.