0

I am currently trying to add a category to the comments on my wordpress site. I already added a title to these and tried several ways to add a dropdown (selection) list to the comment form but I am not good in php and I always receive an error msg...

Here is what I am using in functions.php to add the title field:

add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );
function additional_fields () {
echo '<p class="comment-form-title">'.
'<label for="title">' . __( 'Titre (Je peux...)' ) . '</label>'.
'<input id="title" name="title" type="text" size="30"  tabindex="5" /></p>';
}

and here is what I use to save the data:

add_action( 'comment_post', 'save_comment_meta_data' ); 
function save_comment_meta_data( $comment_id ) {
if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') ) 
$title = wp_filter_nohtml_kses($_POST['title']);
add_comment_meta( $comment_id, 'title', $title );
}

Would anybody know how I could adapt these codes to add another meta field as a selection list of, let's say, 2 possible values? I have well read this topic here Add a drop down list to comment form? but I don't undertsand the very long answer...

Thanks a billion times in advance for your help... (I ve been stuck on these for days now) Sincerely,

Thierry

0

OK, I finally made it following this crystal clear tut

Consequently, my functions.php became:

add_action( 'comment_form_logged_in_after', 'additional_fields' );
add_action( 'comment_form_after_fields', 'additional_fields' );

function additional_fields () {
  echo '<p class="comment-form-title">'.
  '<label for="title">' . __( 'Titre (Je peux...)' ) . '</label>'.
  '<input id="title" name="title" type="text" size="30"  tabindex="5" /></p>';

  echo '<strong><label for="category">Cette solution est particulièrement utile...</label></strong>
    <select id="category" name="category">
        <option value=""></option>
        <option value="Tous">Dans tous les cas</option>
        <option value="Argent">Sans argent</option>
        <option value="Temps">Sans le temps</option>
        <option value="Santé">Sans la santé</option>
    </select>';

}


add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
  if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') )
  $title = wp_filter_nohtml_kses($_POST['title']);
  add_comment_meta( $comment_id, 'title', $title );
  if ( ( isset( $_POST['category'] ) ) && ( $_POST['category'] != '') )
  $category = wp_filter_nohtml_kses($_POST['category']);
  add_comment_meta( $comment_id, 'category', $category );

}

I hope it'll help

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.