Skip to content

Instantly share code, notes, and snippets.

@vyatri
Last active December 14, 2015 06:48
Show Gist options
  • Save vyatri/5044991 to your computer and use it in GitHub Desktop.
Save vyatri/5044991 to your computer and use it in GitHub Desktop.
Collection of functions to add karma functionality to your wordpress. So when a post is being voted up, the karma of the author will be up. Just like reddit. This is very basic. Fell free to use and modify.
<?php
/*
to begin, please create a table named [prefix]votelog e.g. wp_votelog
with the following three fields:
id : auto_increment, BIGINT, primary_key
voter_id: BIGINT
post_name: TEXT
*/
/* echo the score of a post */
function the_vote_score($echo = true){
global $post;
$score = get_post_meta( $post->ID, '_vote_score', true );
if ($score == '') {
add_post_meta($post->ID, '_vote_score', 0);
$score = 0;
}
if ($score > 0) {
$score = (string)$score;
$score = '+'.$score;
} else {
$score = (string)$score;
}
if ( $echo === true )
echo $score;
else
return $score;
}
/*
vote a post up. use inside a loop
to vote down, use -1
*/
function voteup($up_by=1){
if ( is_user_logged_in() ) {
global $post, $wpdb;
// already vote?
$user_ID = get_current_user_id();
$is_voted = $wpdb->get_row("SELECT * FROM $wpdb->votelog WHERE voter_id = ".$user_ID." AND post_name = '".$post->post_name."'");
if (count($is_voted) < 1) {
$score = get_post_meta( $post->ID, '_vote_score', true );
if ($score == '') {
add_post_meta($post->ID, '_vote_score', 0);
$score = 0;
}
$score = (float)$score;
$score = $score + $up_by;
update_post_meta($post->ID, '_vote_score', $score);
$wpdb->query(
"
INSERT INTO $wpdb->votelog
(voter_id,post_name)
VALUES (".$user_ID.",'".$post->post_name."')
"
);
// update author karma
$author = $post->post_author;
$karma = get_user_meta( $author, '_author_karma', true );
if ($karma == '') {
add_user_meta( $author, '_author_karma', 0);
$karma = 0;
}
$karma = $karma + $up_by;
update_user_meta( $author, '_author_karma', $karma );
return true;
exit();
}
}
return false;
}
/* check if the current user already voted the post */
function has_user_voted(){
if ( is_user_logged_in() ) {
global $post, $wpdb;
$user_ID = get_current_user_id();
$is_voted = $wpdb->get_row("SELECT * FROM $wpdb->votelog WHERE voter_id = ".$user_ID." AND post_name = '".$post->post_name."'");
if (count($is_voted) > 0) {
return true;
exit;
}
}
return false;
}
/**
* Retrieve a user's karma.
*
* @param int|object User ID, or the entire WP_User object
* @return int the author's karma score
*/
function author_karma($user) {
if ( is_object( $user ) )
$user = $user->ID;
$karma = get_user_meta( $user, '_author_karma', true );
if ($karma == '') {
add_user_meta( $user, '_author_karma', 0);
$karma = 0;
}
return $karma;
}
/* run this if you really need to */
function fix_all_author_karma() {
global $post;
$author = $post->post_author;
if ( !$author ) return;
$meta = get_user_meta( $author, '_author_karma', true );
if ($meta == '') {
add_user_meta( $author, '_author_karma', 0);
}
$new_karma = 0;
$author_posts = get_posts( array( 'author' => $author, 'numberposts' => -1 ) );
foreach ( $author_posts as $p ){
$post_meta = get_post_meta( $p->ID, '_vote_score', true );
if ($post_meta != '') {
$new_karma += get_post_meta( $p->ID, '_vote_score', true );
}
}
update_user_meta( $author, '_author_karma', $new_karma );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment