ThumbsUp is a PHP voting script which allows visitors to vote for anything you want on your website.

Installing ThumbsUp into wordpress only takes a few minutes and generally follows the same instructions given by the author. I’ve made a small demo to show potential areas you can rate.

For the sake of simplicity this tutorial assumes you are up to the Uploading ThumbsUp step.

Uploading ThumbsUp

Use your favorite FTP application to upload the whole “thumbsup” folder to your wordpress directory

Including ThumbsUp in wordpress

Add the following function into your wordpress themes function.php file.

function add_thumbsup(){
      include './thumbsup/init.php';
      echo ThumbsUp::css();
      echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>';
      echo ThumbsUp::javascript();
}
add_action('wp_head', 'add_thumbsup');

Note: If you are getting init.php errors try replacing the relative path used above ./thumbsup/init.php with your full server path.

Whats it do you ask? This function hooks into wp_head adding the required css and jquery into our themes header.

Now we can add create ThumbsUp items on anything in wordpress with a unique identifier such as wordpress posts, comments, pages and taxonomies.

Creating ThumbsUp Items

In your themes single.php you can add the thumb anywhere in the post loop using a rating describer (I used post_) and your posts id as the item name.

<?php $ThumbUpID = 'post_'.get_the_ID(); echo ThumbsUp::item($ThumbUpID)->template('thumbs_up_down')->options('align=left'); ?>

Note: I’ve used post_ with post id as an item name to further identify what is being rated. You will want to do this especially if you want to use ThumbsUp with other items otherwise you take the risk of having conflicting item names.

Get posts by rating

Add the following function into your themes function.php file. The function returns results based on parameters given which you will need to loop through to display in wordpress.

function get_thumbs_posts($orderby='votes_balance', $limit='10', $sort='DESC', $type='post') {
	global $wpdb;
	$limit = $limit;
	if(!empty($limit)){
		$limit = 'LIMIT '.$limit;
	}
	$results = $wpdb->get_results("SELECT DISTINCT * FROM $wpdb->posts INNER JOIN (SELECT *, SUBSTRING(name, 6) as 'post_ID', votes_up - votes_down AS votes_balance, votes_up + votes_down AS votes_total FROM thumbsup_items) AS thumbsup ON $wpdb->posts.ID = thumbsup.post_ID WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = '$type' AND $wpdb->posts.post_password = '' ORDER BY $orderby $sort $limit");
	return $results;
}

Note the function parameters

  • orderby – votes_total, votes_balance [default]
  • sort – ASC, DESC [default]
  • limit – use to limit results by a number, 10 [default]
  • type – page, post [default]

Important: This function will only work correctly if your rating describer is a 5 letter string, in this tutorials case post_ is so it will work fine.

Loop Example

The example demo loops through the results to show the top 3 posts in the sidebar. This is done by calling our function to get the result and looping through it.

<?php
   $thumblist = get_thumbs_posts('votes_balance', '3');
   foreach ($thumblist as $post) :
   $url = get_permalink( $post->ID );
?>
<li><a href="<?php echo $url ?>"><?php echo $post->post_title ?></a></li>;
<?php endforeach; ?>

If you have a better way of doing this or have a suggestion feel free to post a comment below.

Retrieving Users WordPress ID

Note: I am no php expert however I found this works for me.

In your config.php file add the following above the array:

function get_userid(){
  global $user_ID;
  return $user_ID;
}

Then make sure you set your user_id_callback in your config.php to:

'user_id_callback' => 'get_userid',

You may also want to set ‘user_id_check’ to TRUE.

Now go into classes/thumbsup.php and add before the class declaration:

require_once('/yourwordpresspath/wp-load.php');

Note: Replace the path with your own.

Now you should notice that when wordpress registered users vote their ID will be recorded in the thumbsup_votes table. Which you can use [with a sql query and loop] to show posts users have voted on.