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'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?

share|improve this question
    
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 May 28 '12 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 May 29 '12 at 8:20
add comment

migrated from stackoverflow.com May 28 '12 at 17:11

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 0 down vote accepted

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 http://stackoverflow.com/questions/4798001/insert-into-mysql-database-with-jquery-and-php to get a form to submit.

share|improve this answer
    
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 May 29 '12 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 May 29 '12 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 May 31 '12 at 15:37
add comment

Your Answer

 
discard

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.