New auto vote code on publish
-
i have tweaked some code in another post to auto vote up pages upon a publish.
add_action('save_post', 'auto_rate_post'); function auto_rate_post($post_id) { if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } if(!isset($_POST['post_type']) || !current_user_can('edit_post', $post_id)) { return $post_id; } $stars = 30; $total_stars = is_numeric(get_option('kksr_stars')) ? get_option('kksr_stars') : 5; if(!get_post_meta($post_id, '_kksr_ratings', true)) { $ratings = $stars / ($total_stars/5); $avg = $ratings ? number_format((float)$ratings, 2, '.', '') : 0; update_post_meta($post_id, '_kksr_ratings', $ratings); update_post_meta($post_id, '_kksr_casts', 6); update_post_meta($post_id, '_kksr_avg', $avg); } return $post_id; }
the code above basicly gives 6x 5-star votes to a page on publish.
to modify for yourself think of the total number of votes you want the post to start off with, i in this example 22, then figure out what rating you want the post to average (22 VOTES x 4.8 final rating =106 STARS TO ADD)
the final result on the website comes to
4.77/5 (95.45%)
22 votesadd_action('save_post', 'auto_rate_post'); function auto_rate_post($post_id) { if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } if(!isset($_POST['post_type']) || !current_user_can('edit_post', $post_id)) { return $post_id; } $stars = 106***TOTAL STARS TO ADD (22 VOTES X 106 STARS); $total_stars = is_numeric(get_option('kksr_stars')) ? get_option('kksr_stars') : 5; if(!get_post_meta($post_id, '_kksr_ratings', true)) { $ratings = $stars / ($total_stars/5); $avg = $ratings ? number_format((float)$ratings, 2, '.', '') : 0; update_post_meta($post_id, '_kksr_ratings', $ratings); update_post_meta($post_id, '_kksr_casts', 22***THIS NUMBER IS WHERE YOU PUT THE NUMBER OF VOTES (22 VOTES X 106 STARS); update_post_meta($post_id, '_kksr_avg', $avg); } return $post_id; }
the exact cut and past code for
4.77/5 (95.45%)
22 votes is:function auto_rate_post($post_id) { if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } if(!isset($_POST['post_type']) || !current_user_can('edit_post', $post_id)) { return $post_id; } $stars = 105; $total_stars = is_numeric(get_option('kksr_stars')) ? get_option('kksr_stars') : 5; if(!get_post_meta($post_id, '_kksr_ratings', true)) { $ratings = $stars / ($total_stars/5); $avg = $ratings ? number_format((float)$ratings, 2, '.', '') : 0; update_post_meta($post_id, '_kksr_ratings', $ratings); update_post_meta($post_id, '_kksr_casts', 22); update_post_meta($post_id, '_kksr_avg', $avg); } return $post_id; }
to use the above auto voting, just add the code to your functions.php file
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘New auto vote code on publish’ is closed to new replies.