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
-
i will be trying to tackle the voting up of old posts soon, so if anyone has a solution to set the votes, or generate the data, please let me know.
im considering researching how to trigger the function above with a different action other than page publish.
im considering imacros for an automated republish of all pages.
ill look for a plugin solution that republishes all pages.
and ill see if i can get help on overstack for a different idea.
please let me know if you have any tricks you use.
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 = 60; $kkfaketotal = rand(12,15); $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', $kkfaketotal); update_post_meta($post_id, '_kksr_avg', $avg); } return $post_id; }add random user $kkfaketotal = rand(12,15);
min=4 / max=5
Hi.
I ‘ve already add this code into my theme-functions.php and already create a user kkfaketotal. Now, how can i do this work? Is it possible to rate all posts randomly?
TIA
The topic ‘New auto vote code on publish’ is closed to new replies.