-
-
Save Dudo1985/9b74dfcbfe712af8f0bacada4d6a0af1 to your computer and use it in GitHub Desktop.
This will replace the rating value of yasr_visitor_votes showing the rating of the current user if logged in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter('yasr_vv_shortcode', static function ($shortcode_html, $stored_votes) { | |
//if user is not logged, return shortcode | |
if(!is_user_logged_in()) { | |
return $shortcode_html; | |
} | |
$post_id = get_the_ID(); | |
$current_user_rating = YasrDatabaseRatings::visitorVotesHasUserVoted($post_id); | |
//if current user has not rated, return shortcode | |
if($current_user_rating === false) { | |
return $shortcode_html; | |
} | |
$number_of_votes = $stored_votes['number_of_votes']; | |
if ($number_of_votes > 0) { | |
$average_rating = $stored_votes['sum_votes']/$number_of_votes; | |
} else { | |
$average_rating = 0; | |
} | |
$average_rating=round($average_rating, 1); | |
$search = 'data-rating=\''.$average_rating.'\''; | |
$replace = 'data-rating=\''.$current_user_rating.'\''; | |
//ovewrite and return | |
return str_replace($search, $replace, $shortcode_html); | |
}, 10, 2); |
Thank you for sharing this!
I've made some minor changes to your code:
add_filter('yasr_vv_shortcode', static function ($shortcode_html, $stored_votes) {
if(!is_user_logged_in()) {
//this will overwrite stars!
return 'YOUR TEXT HERE';
}
$post_id = get_the_ID();
$current_user_rating = YasrDatabaseRatings::visitorVotesHasUserVoted($post_id);
$number_of_votes = $stored_votes['number_of_votes'];
if ($number_of_votes > 0) {
$average_rating = $stored_votes['sum_votes']/$number_of_votes;
} else {
$average_rating = 0;
}
$average_rating=round($average_rating, 1);
$search = "data-rating='".$average_rating."'";
$replace = "data-rating='".$current_user_rating."'";
//no need if-else here, since $current_user_rating has the proper value
return str_replace($search, $replace, $shortcode_html);
}, 10, 2);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks
another version if you want that connected users only vote without seeing the average