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 have managed to make my own custom comment rating system - that was working fine until now. I got an idea: why not add star rating to individual comments? I thought it would be easy as I have already added a custom rating field in comment - through filter that is ..

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

function additional_fields ($below) {
    $ratsec = '<p class="comment-form-rating">'.
    '<label for="rating">'. __('Rating') . '<span class="required">*</span></label>
    <span class="commentratingbox">';

        //Current rating scale is 1 to 5. If you want the scale to be 1 to 10, then set the value of $i to 10.
        for( $i=1; $i <= 5; $i++ )
        $ratsec .= '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"/>'. $i .'</span>';

    $ratsec .= '</span></p>';
$below = $below .$ratsec;
return $below;

But when I change things and add this filter

add_filter( 'comment_text', 'additional_fields' );

it starts showing the radio button in every comment just below the user text - but how to submit them? As along with comment you have submit button ?

My first Question is: How I can make the rating submit ?

My Second Question is: If I vote 3 star for "comment number one" will it affect all the comments or just the one I voted? I am confused how to make connection between them.

I was using comment meta data to save the data that was ...

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

Now the Code is completely changed How will I save the data in comment meta data to make it show on the front end ? What Modifications are required in the above action ? Plus now using the perfect method explained below by @s_ha_dum -- How will I store this data according to comment id ?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

This is almost entirely an HTML and/or Javascript question. You just need to make proper forms and have some PHP to process it.

function additional_fields ($below) {
  global $comment;
  $ratsec = '<form action="'.get_permalink().'" method="get" class="comment-form-rating">';
    $ratsec .= '<input type="hidden" name="p" value="'.get_the_ID().'"';
    $ratsec .= '<label for="rating">'. __('Rating') . '<span class="required">*</span></label>';
    $ratsec .= '<span class="commentratingbox">';

      //Current rating scale is 1 to 5. If you want the scale to be 1 to 10, then set the value of $i to 10.
      for( $i=1; $i <= 5; $i++ ) {
        $ratsec .= '<span class="commentrating"><input type="radio" name="rating['.$comment->comment_ID.']" id="rating" value="'. $i .'"/>'. $i .'</span>';

      $ratsec .= '<input type="submit" name="rate" value="Rate" />';
    $ratsec .= '</span>';
  $ratsec .= '</form>';
  $below = $below .$ratsec;
  return $below;
add_filter( 'comment_text', 'additional_fields' );

You now have a form that will submit and in the GET string you should see your comment ID.

Use CSS to format it and if you want use Javascript to hijack the form submission entirely, so that you don't need the submit button at all (hide it with Javascript but keep it in case Javascript is disabled). For that see the AJAX API.

The following will save the data.

function save_comment_meta_data() {
  if ( !empty($_GET['rating'])) {
    foreach ($_GET['rating'] as $k => $v) {
      if (!ctype_digit("$k")) {
      } else {
        $k = (int)$k;
      $comment = get_comment($k);
      if (!empty($comment)) {
        $rating = wp_filter_nohtml_kses($_GET['rating'][$k]);
        add_comment_meta( $k, 'rating', $rating );
add_action( 'wp_head', 'save_comment_meta_data' );
share|improve this answer
Its doing strange things - when i press submit it redirect me to unknown page itokra[dot]com/design/?rating%5B7%5D=2&rate=Rate –  Cid Ubaid Jun 4 '13 at 18:17
@CidUbaid : see the edit –  s_ha_dum Jun 4 '13 at 18:30
same error redirecting to same place. –  Cid Ubaid Jun 4 '13 at 18:54
What is an URL to one of your posts/pages that have comments? –  s_ha_dum Jun 4 '13 at 18:57
itokra[dot]com/design/?p=33 –  Cid Ubaid Jun 4 '13 at 18:58
show 5 more comments

You could add a metabox to your comment. For this you can use some hooks such as preprocess_comment() and comment_post() which will allow you to save rating along with comments. This will also add some meta in your database.

share|improve this answer
I have already added them but Now I want to know - that I am displaying them on every comment - I want them to be more interactive that author can vote them so I want to know how I can submit them ? –  Cid Ubaid Jun 4 '13 at 17:38
add comment

Your Answer


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.