Theming Drupal's Vote Up/Down Module
I guess quite a few people are looking to integrate some voting mechanism into their sites these days, so here's a quick guide to theming Drupal's vote up/down module Digg style. Btw, thanks to Chad C for the suggestion, and if anyone else has ideas for posts/tutorials they'd like to see on My Drupal Blog feel free to just post them in the comments or drop me an email.
For this tutorial I'll be using the vote up/down module 5.x-1.x-dev on Drupal 5.2 and the default Garland theme.
This tutorial assumes that you have already installed and enabled the other necessary voting modules – in this case the voting API module 5.x-1.5. If not, refer to the install.txt and readme.txt files packaged with the vote up/down module for further help.
The Digg button
Well, Digg is the classic voting button after all, so lets look at how you'd theme Drupal's vote up/down package to look just like the Digg button. Basically, we'll take the default Drupal look (left) and turn it into the Digg look (right).

Step 1: Setup
First thing you need to do is go into the admin vote up/down settings (Administer > Site configuration > Vote up/down).
Set the node type you want the voting widget to show up on, set the 'Vote widget style' to alternative, and then disable the 'Link display of vote points'.

Step 2: The Images
OK, second thing you're going to need are three images:
- one for the count/number of diggs background
- one for the digg it button background when inactive
- one for the digg it button background when hovered over
For the purposes of this example I'm using the images from Digg, but you can use any images you like.

Now, just save these images to your theme's 'images' folder as normal.
Step 3: style.css
Open your style.css file and add the following:
.vote-up-down-widget-alt .vote-points {
height: 44px;
width: 51px;
margin: 0 0 3px 0;
padding: 8px 0 0 0;
color: #736926;
font-family: Arial, Helvetica, sans-serif;
background: #fff url(images/count_bg.jpg) no-repeat;
}
.vote-up-down-widget-alt .vote-up-inact:hover {
background: url(images/diggit_hover_bg.jpg) no-repeat;
}
.vote-up-down-widget-alt span.vote-up-act, .vote-up-down-widget-alt span.vote-up-inact, .vote-up-down-widget-alt span.up-inact {
border: none;
}
.vote-up-down-widget-alt .vote-up-inact, .vote-up-down-widget-alt .up-inact, .vote-up-down-widget-alt .vote-up-act {
background: url(images/diggit_bg.jpg) no-repeat;
}
.vote-up-down-widget-alt .vote-up-act, .vote-up-down-widget-alt .vote-up-inact, .vote-up-down-widget-alt .up-inact {
width: 51px;
height: 25px;
}
.vote-up-down-widget-alt .vote-points-label {
font-size: 63%;
line-height: 100%;
color: #998d43;
}Obviously, you will need to replace the various styles for height, width, color, and image with ones that relate to your particular site.
One important thing to note is that positioning/aligning the '0 points' text vertically can be a little tricky. I have done it by:
- adding a top padding of 8px to the .vote-up-down-widget-alt .vote-points div to position the text nicely
- subtracting this amount (8px) from the actual height of my count_bg.jpg (52px) file to give the real height you need to enter in the style.css (in this case 44px).
This extra calculation is required to help the digg it button sit correctly underneath.
All being well, you should now have the following:

Step 4: template.php
OK, that's looking pretty good but we now need to override the actual vote up/down module file functions which output 'points' and change it to 'diggs', so open your theme's template.php file and add the following:
/**
*Override vote up/down. Change 'points' to 'diggs'.
*/
function garland_vote_up_down_points($cid, $type, $nodelink = FALSE) {
$vote_result = votingapi_get_voting_result($type, $cid, 'points', variable_get('vote_up_down_tag', 'vote'), 'sum');
if ($nodelink) {
if ($vote_result) {
$output = array(
'title' => '<span id="vote_points_'. $cid .'" class="vote-points">'. format_plural($vote_result->value, '1 digg', '@count diggs') .'</span>',
'html' => TRUE
);
}
else {
$output = array(
'title' => '<span id="vote_points_'. $cid .'" class="vote-points">'. t('0 diggs') .'</span>',
'html' => TRUE
);
}
}
else {
if ($vote_result) {
$output = '<span id="vote_points_'. $cid .'" class="vote-points">'. format_plural($vote_result->value, '1 digg', '@count diggs') .'</span>';
}
else {
$output = '<span id="vote_points_'. $cid .'" class="vote-points">'. t('0 diggs') .'</span>';
}
}
return $output;
}
function garland_vote_up_down_points_alt($cid, $type) {
$vote_result = votingapi_get_voting_result($type, $cid, 'points', variable_get('vote_up_down_tag', 'vote'), 'sum');
if ($vote_result) {
$output = '<div id="vote_points_'. $cid .'" class="vote-points">'. $vote_result->value;
}
else {
$output = '<div id="vote_points_'. $cid .'" class="vote-points">0';
}
$output .= '<div class="vote-points-label">'. format_plural($vote_result->value, 'digg', 'diggs') .'</div></div>';
return $output;
}…and basically that's it.
Just remember when using the above snippet to:
- replace 'garland' with the name of your own Drupal theme folder
- replace each occurrence of 'digg' and 'diggs' with your own points name (e.g. spins, goals, boosts, votes etc.) using either the singular or plural as appropriate





Hi, I'm Laurence and this is my Drupal blog.
Don't Make Me Think!
Pro Drupal Development
PHP Cookbook
Will You Please Be Quiet, Please?
Nice trick :)
Maybe you could create more unique visual effect than copy of the "digg it".
However, nice.
Thanks,
yes I should have made clear that I'm not suggesting you should copy Digg! (not unless you enjoy threatening letters ;)
I was just using Digg as it is the most well known example of a voting button, but you can adapt the methods above to pretty much whatever design you've got going on your website.
Nice! Glad to see this and you explained it perfectly. Great addition on showing how to correctly change the "points" text.
Another idea for a tutorial that seems to clog the Drupal Forums is a cool way to display Advertisement like Adsense.
HTML Buzz would handle an adsense section like http://digg.com/about
has quite well. Custom Region? Great Tutorials!
Hi Chad,
glad you like the tutorial :)
I wrote a post a few months ago on creating custom regions in Drupal, which might be useful to you with regards to adsense placement.
What do you think about using
tags for the voting widget instead of Divs? Curious because thats what most of the voting sites use.
That was not supposed to say *tags* btw, but supposed to say Un-Ordered List, UL
There is a bug with this method, when you click over "digg it" word to cast a vote, the div is moved down, if you keep click the div is moved down again and again.. the problem is vertical alignment using the padding method, any ideas on how to solve it?
regards,
Nice stuff!
Post new comment