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.
Thanks for the guide. I was looking for a better tutorial then the one provided for this on code canyon. Thanks!!!!
No worries I thought this would be useful for others, glad I could help.
I’ve added the necessary code to my functions.php file, and I am able to get the individual posts to display the ThumbsUp, but I am not able to display the top rated posts on my homepage or in my sidebar … I have copied your get_thumbs_posts function exactly as you have it here and I even have the rating describer set to post_ as you have in your example. Any ideas on what I may be doing wrong?
I just noticed a mistake in my loop which im guessing is why yours is not working. Recheck your $thumblist call.
It should be this for top 3: $thumblist = get_thumbs_posts(‘votes_balance’, ’3′);
I’m wondering if anyones been able to do the following;
Retrieve popular posts within the last (24hours / 7 days / month / all time) – Published within x amount of time.
And;
Linking the voting to the wordpress memberbase so you can put on user profile, User x voted on ‘post titile’.
Would this be possible with the script?
Hey Chris, all those would be possible, just requires you to play around with the code.
For example: if we were linking votes to members/users we would need to record their userid in the thumbsup_votes table and then use a sql query to pull the data and loop through it.
Wow thanks Peter for the great start in helping. I see that i just need to play around with the code, I’m no expert at php either but i can make sense of it usually so your updates are a great help.
much appreciated.
Hello! Thank you for this guide! Ia followed your instructions, but after I added the code into functions.php, I get this warnings on my page:
Warning: include(./thumbsup/init.php) [function.include]: failed to open stream: No such file or directory in /home/lowdowno/public_html/wp-content/themes/anarchy-wall-10/functions.php on line 6
Warning: include(./thumbsup/init.php) [function.include]: failed to open stream: No such file or directory in /home/lowdowno/public_html/wp-content/themes/anarchy-wall-10/functions.php on line 6
Warning: include() [function.include]: Failed opening ‘./thumbsup/init.php’ for inclusion (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/lowdowno/public_html/wp-content/themes/anarchy-wall-10/functions.php on line 6
Fatal error: Class ‘ThumbsUp’ not found in /home/lowdowno/public_html/wp-content/themes/anarchy-wall-10/functions.php on line 7
Could you maybe help me? Thank you!
Hi coby sounds like you haven’t uploaded the thumbsup file or you haven’t put it in the correct location.
thumbsup folder should be located in your wordpress directory.
Is this tutorial for version 1.1.0? Thanks. I do not see a init.php file.
No this is for v2. I suggest you update.
Hi Peter,
I am also getting the same errors as cobydick.
I have wordpress in my root directory and I have have thumbsup in the root as well.
It’s this part: include ‘./thumbsup/init.php’;
thats messing it up I think because ./ is relative to the current directory which for me is
wp-content/themes/my theme/functions.php thats where I am entering the code, correct?
I’m new to all of this so sorry if I’m being stupid…
So do I have to put the thumbsup folder in my theme directory or what do I change the structure to?
I anxiously await your reply
Thanks
Oh, another thing….
Is it possible to display thumbsup in pages as well?
Thanks in advance
Hi Dan, it is possible to display thumbsup in pages using the same code as shown for posts.
Thumbsup folder should be in your wordpress directory. The same directory as your wordpress config file etc.
You can try putting in your full path in place of ./thumbsup/init.php which would look something like /home/username/public_html/thumbsup/init.php
Let me know if that helps.
Thank you Peter, that worked
Although I’m stuck on how to use code on pages…
For posts we edit single.php but I don’t know what file edits pages :-/
I tried putting the code straight intop the wordpress editor with a plugin enabled that allows php but it just bought up errors.
Could you help me?
Thanks, Dan
P.S: Nice blog btw and a great tutorial.
Sorry for being such a pain but could you also confirm something for me.
To get the minithumbs beside comments do you add the code to comments.php? and is their any extra code needed?
Once again, thanks for the help and tutorial as thier is no official support for intergrating with wordpress.
Heres the code I use for comments:
< ?php $thumbsupid = "comm_".get_comment_ID(); echo ThumbsUp::item($thumbsupid)->template('mini_thumbs')->options('align=left');?>
Where you place the code depends on your theme. Try looking for class=”comment-author vcard” inside your comments.php or functions.php and place the comment code above it or inside it (depends on how your theme is styled).
To get the thumbs working on pages just edit your pages.php file and add the relevant code within the page loop.
Sorry for double posting.
Here is the code I found
<input type="text" name="author" id="author" value="" size="32" tabindex="1" />Name
I tried adding the thumbsup code here but no thumbs showed up in comments.
<input type="text" name="email" id="email" value="" size="32" tabindex="2" />E-Mail
<input type="text" name="url" id="url" value="" size="32" tabindex="3" />Website
Does it only show thumbsup on comments that get posted after I have inserted the code? If so, I might have put the code in right place? It just needs new comments?
Thanks
The above is not the correct code. For some reason the code has been cut off… I used the
tag as well, weird…Basically this is the main part: php echo $comment_author;
I removed the php tags.
Hi Dan if you like I can take a look at it, email me your details at peter@waspmedia.co.nz. Once you put the code in the correct place it will show for all comments new and old.
I found myself ndoindg my noggin all the way through.
d88Gs7 xkyedilhudhz
Great tutorial, I am sure everyone appreciates your efforts, I know they’ve helped me very much.
How would you display the most voted posts from a certain time period?
The following snippet should set the time:
<?php $mylimit=30 * 86400; //days * seconds per day$post_age = date('U') - get_post_time('U');
if ($post_age
the problem is, how should I integrate with Thumbsup so it shows most voted from that time period? Also, is the post limit mandatory?
Thanks.
I ran this code and it’s not displaying the “Top Articles” at all. Any idea?
thanks for this tutorial…
i am however having some strange problem…
i placed this:
include (‘/home/useraccount/public_html/photography/thumbsup/init.php’);
in my functions.php and it includes just fine..
however i get this in my source:
and inside that is the 4040 page of wordpress saying it cannot find init.min.js.php
what could be going wrong? thanks…
sorry my bad… got it working now.. thanks…
Hi Peter,
thanks for this guide!
I have a problem when i try to create a thumbsup item. I don’t understand the “post_” and where i have to put the post id and item name in this code, template(‘thumbs_up_down’)->options(‘align=left’); ?>
Could you make an example? I’m not a programmer and i’m gonna be crazy!
Please, can you help me?
Thank you very much for your patience
Hi polz you dont need to mess around with adding post_ as ive already added it in the code:
< ?php $ThumbUpID = 'post_'.get_the_ID(); echo ThumbsUp::item($ThumbUpID)->template('thumbs_up_down')->options('align=left'); ?>sorry I leaved some code on my textedit
i’m talking about this code: template(‘thumbs_up_down’)->options(‘align=left’); ?>
I’ve added everything as per instructions – but I get the following error:
PHP Fatal error – when I load the page – class thumbsup not found on line 11.
I’ve tested everything I know, but can’t figure this out
if you visit my site > http://socialmediaseo.net and open any article, you’ll see the content missing below the title.
that’s where my error is
any thoughts?
Fatal error: Class ‘PDO’ not found in …/public_html/wordpress/thumbsup/classes/thumbsup.php on line 65
Thanks for this awesome guide!
Question: All I’m trying to do is place a Thumbs Up icon next to pieces of text on a WP page on my site (just allowing users to vote on each line of text). Would I be able to do this using this script, or does it only work with use in posts/comments/etc.?
no matter what i try i cannot log in to my admin .
i use this username :- demo and password:-89e495e7941cf9e40e6980d14a16bf023ccd4c91
please help
Drew, I believe the “89e495e7941cf9e40e6980d14a16bf023ccd4c91″ is actually “demo”, it was just encoded using SHA1 HASH, go here to encode your own password , simply put your password in the box and submit.
I can’t hear you very well black loli naked pic 611
Hello Peter, quick question, how would I go about creating the TumbsUp items without just using the Post ID, but rather the post name, custom post type and custom post type category?
I was able to add the post title and custom post type, however, I’m having trouble adding the category. Here’s the code I’ve used:
template('thumbs_up')->options('align=left'); ?>template('thumbs_up')->options('align=left'); ?>template('thumbs_up')->options('align=left'); ? >, of course without the php open and closing spaces.Sorry for all the posts, the code is not displaying, I’ll just post the part I changed:
'post_'.get_the_ID().'_'.get_the_title().'_'.get_post_type();I was able to figure it out, solution: here.
Hey Erick, glad you figured it out. Didn’t realize people were still commenting on this post.
How do you enable only registered users to vote. Please kindly share
Hi Stan, read the Retrieving Users WordPress ID section.
Hey mate, was hoping you could help me out.
This advice works great, except the echo to call jquery means I have multiple jquery scripts running on the page (which results in the other scripts from plugins not working). If I take out the echo, then of course thumbsup doesn’t function or look as intended. What do I need to change to get the jqueries working together?
Hi David, If you remove the jquery call make sure your sites normal jquery library link is above all your jquery scripts. Im assuming the reason thumbsup wont work when you remove the jquery call is because your normal jquery link is below the thumbsup javascript.
Hi Peter!
Is it possible to replace the usual wp posts query with you solution to get all of the posts ordered by votes? With paged support etc
Regards Roman
Hi Roman,
Yes that is possible. You would need to add a filter to modify the posts loop WHERE clause. Have a look at http://codex.wordpress.org/Custom_Queries for a good example.
How can I create a voting list? A wordpress page/post where people can vote items like ” top 100 …..” and the items can go up/down based on the number of votes.
PLEASE HELP.
I do NOT want to vote posts and create a list of top post. I need to vote items ( a list of items) on a post/page.
Thanks
Excellent tutorial. Can you post a guide as well for working this out on simple dv boxes with different contents retrieved from mysql? Thanks!
Hi Peter, very nice tutorial!
I’ve downloaded: 1.3.8 – 2011-12-16
But there is no ./thumbsup/init.php
file in that version.
Any suggestions?
Thanks in advance!
Hi again,
Forgot my first question, solved that one myself.
Now issue nr. 2:
I want to place the votebar right under every post, so i think i have to add the ‘thumbsup’ code to my single.php theme file right?
When i add this line:
template(‘thumbs_up_down’)->options(‘align=left’); ?>
to single.php i got this error when entering a page:
Fatal error: Class “ThumbsUp not found in /home/website/domains/website.com/public_html/wp-content/themes/theme/single.php on line 28
How to solve this one?
thanks again!
Hi Mark, Yes you will need to add the thumbsup code to your single.php theme file.
It sounds like ThumbsUp is not being initialised meaning you haven’t included the thumbsup init file properly.
Thanks for your reply Peter, the include was not right indeed. its working great now.
Great support!
Mark
Hi again
One last question:
When a visitor has voted, the sign ‘thanks for your vote’ is not showing up. After another click on the button (that 2nd click is not registated as a vote) the ‘thanks for your vote’ singn is there.
The person on that computer can not vote agian at that moment, only after a few hours, voting is posible again.
Is there a posilbilty to allow a single IP / PC / Person only one vote ?
Mark
Great function, i was wondering how could i add the votes total to each post returened?
Example:
title one(12)
title two(11)
title three(5)
The number in the brackets being the amount off votes. I managed to edit a template but it only returns the same value for all posts.
[...] Third party tutorial on how to install ThumbsUp in WordPress. [...]
Hi Peter! I am using Headway Themes 3.1.2 and I made my own custom theme. Right now all it has is a functions.php and no single.php in my child theme folder. Do I simply add a file named “single.php” and then add in the code?
[...] http://wpfan.com/how-to-install-thumbsup-in-wordpress/ [...]
Great tutorial,
I used it to show thumbs in comments , but I have problem to filter and display top voted comments , tried to modify your code that display the top rated post in the side bar but no success, if you still checking this post please help
thanks
Hi Peter,
I think I did everything correctly, but when I try to add a item I get the following error,
Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000] [2013] Lost connection to MySQL server at ‘reading initial communication packet’, system error: 111′ in /nfs/c08/h02/mnt/121936/domains/fear-monger.com/html/thumbsup/classes/thumbsup.php:70 Stack trace: #0 /nfs/c08/h02/mnt/121936/domains/fear-monger.com/html/thumbsup/classes/thumbsup.php(70): PDO->__construct(‘mysql:dbname=db…’, ‘db121936_fear’, ‘marley0841′, Array) #1 /nfs/c08/h02/mnt/121936/domains/fear-monger.com/html/thumbsup/classes/thumbsup_item.php(121): ThumbsUp::db() #2 /nfs/c08/h02/mnt/121936/domains/fear-monger.com/html/thumbsup/classes/thumbsup.php(214): ThumbsUp_Item::load(‘standup’) #3 /nfs/c08/h02/mnt/121936/domains/fear-monger.com/html/wp-content/themes/reverie/main-page.php(244): ThumbsUp::item(‘standup’) #4 /nfs/c08/h02/mnt/121936/domains/fear-monger.com/html/wp-includes/template-loader.php(43): include(‘/nfs/c08/h02/mn…’) #5 /nfs/c08/h02/mnt/121936/domains/fear-monger.com/html/wp-blog-header.php(16): require_once(‘/nfs/c08/ in /nfs/c08/h02/mnt/121936/domains/fear-monger.com/html/thumbsup/classes/thumbsup.php on line 70
Any idea what’s going on here?
Hi,
I just downloaded ThumbsUp and installed it on WordPress as per the instructions above (thanks a ton for taking the trouble to help by the way!).
I have the plugin installed and the thumbs up and down icons are appearing correctly.
When I click on either button, it even adds a new row correctly in the thumbsup_items table in my DB.
However after clicking either of the buttons, the count for both Vote Ups and Vote Downs display as “parser error”, and the count is not updated on the database.
Any idea what could be causing this issue?
Clearly the DB is connecting correctly, the init.php file is fine too.
I am at a bit of a loss!
Would greatly appreciate any help – thanks very much in advance!
Best regards,
Prakash
Thanks for the awesome tutorial! What is the best way to get this to work on a custom_post_type?
I’m using this: template(‘thumbs_up’)->options(‘align=center’); ?>, in my single page view, but it displays both the “thanks for your vote” and “vote” buttons by default, then when clicking on “vote” nothing happens, I just get an eternal spinner. When I refresh, I’m back to square one.
Am I missing something?
Thanks so much for any help!
Hi Peter
Great article! I’ve trying to work this out for hours and your article puts it so simply!
So many people were in need of a simplified WordPress plugin or set of instructions and the author didn’t seem to get onto it.
I’ve finally got the +1 -1 buttons working but it doesn’t seem like the CSS is working. I have copied just as above…
template(‘thumbs_up_down’)->options(‘align=left’); ?>
Yet the thumbs_up_down just displays as regular buttons
Any help would be hugely appreciated!!
Your INNER JOIN should be a LEFT JOIN a la http://stackoverflow.com/questions/7588612/how-to-have-sql-inner-join-accept-null-results
The rest of the post was really helpful. Thank you.
Great tutorial! Very useful.
I feel like this should all be wrapped up into a plugin to make it easier to insert for WordPress.
Hmm…