BellaBook 3.5: Implementing Gravatars
A gravatar, otherwise known as the "globally recognised avatar", is an image/icon tied in to a person's email address that can be customised to show up on all sorts of scripts. This tutorial explains how to implement gravatars into BellaBook 3.5.
First, you should save any BellaBook files that you've customised to your computer so that if something goes wrong, you can easily start again, and so that we can work on the files offline. When you've saved your guestbook files, make a second copy and put it somewhere safe — these can be the "just in case" files. Working with the originals, open config.php and find (near the bottom of the file):
} blanklinefix(ENTRIES);
Above blanklinefix(ENTRIES); but below the } (inbetween the two) add:
function doGravatar($email) {
$default = "http://www.your-site.com/guestbook/avatar.jpg"; // default avatar when none is found
$size = 40; // anything between 1 - 80
$rating = "R"; // G, PG, R or X (X-rated avatars may contain nudity/etc)
$grav_url = 'http://www.gravatar.com/avatar.php?gravatar_id='.md5($email).'&rating='.$rating.'&default='.urlencode($default).'&size='.$size;
echo '<img src="'.$grav_url.'" alt="" />';
}
This function constructs a URL which asks gravatar.com to return the image (avatar) associated with the email address we give it — in our case it will be the email address of each person signing the guestbook. Customise the $default, $size and $rating variables to suit your preferences, just like you did in prefs.php when you first installed BellaBook.
Now open index.php, and find:
list($name,$email,$url,$odate,$ip,$message) = preg_split("/,(?! )/",$entries[$i]);
This pulls each entry from our guestbook file and splits it. Further on in the script we turn e-mail into a bit of JavaScript to dissuade spammers, so to make the gravatars work on the original e-mail address we need to create a separate variable. After the line above, add:
$origemail = $email;
Now find:
<td class="meta"> <img src="user.gif" alt="" /> <span class="bold">Name:</span> <?php echo $name; ?><br /> <?php echo $email; ?> <img src="www.gif" alt="" /> <span class="bold">Website:</span> <?php echo $url; ?><br /> <img src="date.gif" alt="" /> <span class="bold">Date:</span> <?php echo $date; ?><br /> <img src="time.gif" alt="" /> <span class="bold">Time:</span> <?php echo $time; ?><br /> </td>
This part of the script displays the information relating to the message in the guestbook, and would be an appropriate place to display the person's gravatar. If you would like the gravatar to display above the text, add <?php doGravatar($origemail); ?><br /> after <td class="meta">. If you would like the gravatar to display below the text, add <?php doGravatar($origemail); ?> after <?php echo $time; ?><br />.
If you feel comfortable working with HTML and some PHP, you can use <?php doGravatar($origemail); ?> in other places. However, this snippet must remain inside the while loop (starts approx. line 48, ends approx line 85).
When you have finished editing the files, upload index.php and config.php to your guestbook and refresh to see the gravatars in action. If the worst happens and something goes wrong, upload the config/index files from the secondary untouched back-up and start again. Happy gravataring!
Tags:
php, bellabook, gravatar, script-modding,
Last Updated On: 29th March 08 by Jem
Bookmark At: StumbleUpon, Digg

Handy Stuff
Downloads
Friends of 'TT'
Resources