0

I'm writing a Wordpress plugin. The plugin should allow every user to rate posts (1 to 5 stars).

What actually works is to read the rating from storage and show the stars. So there are five <a> links after every post, indicating the ranking by different colours.

The stars are linked because I want to let the visitor rate my post by clicking on one of the stars.

My question is now, how can I prepare my plugin file to receive the link, (save the rating into storage), and sending the user back to the article page. I other words...

How can I run a plugin function if the user follows a link?

2
  • Well you have to do that with ajax. WP have nice ajax implementation. you write function in php and via wp ajax you can triger that function. So i would use wp ajax it sends to my function post ID and rating, so you don't need to know url, or redirect user. w4dev.com/wp/wp_ajax Very easy and nice tut.
    – Tommixoft
    Commented May 28, 2012 at 19:42
  • Ok, I noticed that calling the fuction without AJAX would be a lot more complicated, so I decided to only support javascript users.
    – danijar
    Commented May 29, 2012 at 8:20

1 Answer 1

0

You could, feasibly, do it with a custom field, ignoring ajax. You could store the ratings of each one in an array in the field. The star rating would be generated something like this:

//note this is untested and provided as a guide not gospel
$ratings = get_post_meta($post_ID,'star_ratings');
$total = 0;
foreach($ratings as $rating) {
    $total = $total + $rating;
}
$star_rating = $total/count($ratings);

Then all you need to do is manipulate that.

To store it you just append the new users rating to the array.

To do it with php, you would need to refresh the page to both add the rating and display the new one which could trip the page and/or user out.

EDIT

To place it on the page using a php/jquery update method you could use this:

<div id="ratings">
    <?php
    for($i = 1; $i <= 5; $++) {
        echo '<a class="star" name="'.$i.'">'.$i.'</a>';
    }
    ?>
</div>

And then amalgmate something out of https://stackoverflow.com/questions/4798001/insert-into-mysql-database-with-jquery-and-php to get a form to submit.

3
  • Yes, I already used custom fields and reading the value and showing it in stars works fine. I also have a function called function input_rating($id = 0, $rate) wich stores the visitor's rating in the custom field. But how can I call this function?
    – danijar
    Commented May 29, 2012 at 7:59
  • Going on the following assumptions; a) you want this called when a "star" is clicked, b) $id is the global variable $post_ID and c) $rate is the 1-5 rating of the star/post, you could use a form and place the elements with a for loop or you could try and do it dynamically with a jQuery/PHP fusion thing. I'll add the for loop solution to my answer as off the top of my head I am not sure of the jQuery/PHP solution
    – Joshua
    Commented May 29, 2012 at 23:10
  • That's exactly what I've already done :). I thought there was a simpler way than linking the <a href=""> to the php file of the plugin and redicting the user from there back to the article page. But now it's a simple AJAX solution and I already submitted the plugin to wordpress.org.
    – danijar
    Commented May 31, 2012 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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