0

I would like each and every user to have the rating in stars and a review along with it so that any user can rate them using JavaScript 5 stars. The various reviews along with stars rating should be displayed in the wp-admin panel so that the user can analyze himself. And a cumulative average of star rating should be shown on the profile.

Something similar to the content given in below image. enter image description here

  • I have tried using custom post along with javascript rating with stars but am not sure if it would work?? – Shahrukh Khan Feb 20 '15 at 15:42
2

You can store the ratings as user meta. When a rating is added, add it to the user's ratings using add_user_meta():

add_user_meta( $user_id, '_ratings', $rating, false );

The last parameter tells it to add the current rating to the meta as a new item, not to replace the existing. All ratings can then be retrieved using get_user_meta():

get_user_meta( $user_id, '_ratings', false );

The last parameter tells it to turn the ratings as an array:

Array
(
    [0] => 5
    [1] => 5
    [2] => 3
)

You can loop through these to get the average rating. You may want to calculate and store the average as a separate meta each time the ratings meta is updated, so you can just pull that meta on each page load rather than calculating every time.

You'll probably want to limit users to only rating other users once. Each time a user rates another user you could update another meta that contains a list of user ids that they've udpated:

add_user_meta( $rating_user_id, '_rated', $rated_user_id, false );

You can then check against that before allowing them to rate a user:

$rated = get_user_meta( $rating_user_id, '_rated', 'false' );
if ( ! in_array( $rated_user_id, $rated ) ) {
    // do stuff here to allow rating
}
|improve this answer|||||
  • could you please give me a complete written function – Shahrukh Khan Feb 21 '15 at 9:46

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