WordPress Plugins > WP-PostRatings
Making WP-PostRatings 1.50 Work With SuperCache Properly
lokos:
Hi, Here is how you can make wp-postrating to always show the updated rating with supercache installed
The problem you describe above happen because when you vote your rating refresh via AJAX so you see the updated view count immediately but once the page is refreshed supercache show the cache page with the old rating.
The solution: Load the rating with AJAX
1. Add the AJAX function to get the rating into your theme's function.php file
--- Code: ---function get_rating_ajax($id) {
$nonce = wp_create_nonce('my_rating');
?>
<div id="ratings-wrap-<?php echo $id;?>">
<img src="/wp-content/uploads/images/loading_rating.gif" />
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
$.ajax({
type : "GET",
url : "<?php echo get_option('home'); ?>/wp-content/plugins/wp-postratings/wp-postratings.php",
data : { wp_ajaxaction : "jquery", post_id : <?php echo $id; ?> , _ajax_check : "<?php echo $nonce;?>"},
success : function(response){$("#ratings-wrap-<?php echo $id; ?>").html(response);}
});
});
</script><?php
}
?>
--- End code ---
2. Add the function to return the rating via ajax on the wp-postrating.php file
Look for the line:
--- Code: ---$wpdb->ratings = $wpdb->prefix.'ratings';
--- End code ---
and copy this code below it:
--- Code: ---if ( $_GET['wp_ajaxaction'] == 'jquery' ) {
$nonce =$_REQUEST['_ajax_check'];
if (! wp_verify_nonce($nonce, 'my_rating') ) die('Security check');
$id = $wpdb->escape($_GET['post_id']);
the_ratings('div',$id);
exit();
}
--- End code ---
3. Use it.
Replace your normal <?php the_rating() ?> function in your theme for <?php get_rating_ajax ($post->ID) ?>
Make sure jquery is on the page where you are using this.
You can update this <img src="/wp-content/uploads/images/loading_rating.gif" /> for the path of the loading image you want to show while the rating is being updated.
GaMerZ:
thanks, stickied.
Need_Help:
In which file and where shall I put the codes into?
Thanks.
GaMerZ:
Read the first post carefully, it is all stated there.
Modified the post to make it more obvious
Need_Help:
Thanks for making it clearer. At first, he just stated function.php (which I couldn't find in the plugin folder)
Thanks.
Navigation
[0] Message Index
[#] Next page
Go to full version