Gravatar mod for sNews 1.7
With this mod you can allow your users to add their email address to their comments and use that email address to fetch their gravatar, if they have one. I've been using it here on my site for quite a while now, as you can see.
So without further ado let's get started. As always BACK-UP your snews.php file and work off a copy.
1. find the function comment and add the following highlighted code;
$url = (strlen($url) > 8 && strpos($url, '?') === false) ? clean(cleanXSS($url)) : null;
$email = trim($_POST['email']);
$email = (strlen($email) > 5 && preg_match( '/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i' , $email)) ? clean(cleanXSS($email)) : null;
2. still within the function comment find and make the following highlighted changes;
$query = 'INSERT INTO '._PRE.'comments'.'(articleid, name, url, comment, time, approved, email) VALUES'."('$post_article_id', '$name', '$url', '$comment', '$time', '$approved', '$email')";
3. still within the function comment find and add the following highlighted code
'name'=>$name,
'email'=>$email,
'comment'=>$comment,
4. still within the function comment find and add the following highlighted code, make sure you adjust the rating if you like, and the gravatar size, etc;
case ($tag == 'comment'):
$email = retrieve('email', 'comments', 'id', $r['id']);
$name = $r['name'];
$default = _SITE."/images/gravatar.png";
$size = "48";
$rating = "pg";
$gravatar = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($email)."&rating=".$rating."&default=".urlencode($default)."&size=".$size;
echo '<img class="gravatar" src="'.$gravatar.'" width="'.$size.'" height="'.$size.'" alt="'.$name.'" /> ';
echo $r['comment'];
break;
5. still within the function comment find and add the following highlighted code;
echo html_input('text', 'url', 'url', $url, l('url'), 'text', '', '', '', '', '', '', '', '', '')."\r\n";
echo html_input('text', 'email', 'email', '', l('cemail'), 'text', '', '', '', '', '', '', '', '', '');
echo html_input('textarea', 'text', 'text', stripslashes($comment), '* '.l('comment'), '', '', '', '', '', '5', '5', '', '', '')."\r\n";
echo mathCaptcha()."\r\n";
6. find the function edit_comment and make the following highlighted changes;
$query = mysql_query('SELECT id,articleid,name,url,email,comment,approved FROM '._PRE.'comments'.' WHERE id='.$commentid);
7. still within the function edit_comment find and add the following highlighted code;
echo html_input('text', 'url', 'url', $r['url'], l('url'), '', '', '', '', '', '', '', '', '', '');
echo html_input('text', 'email', 'email', $r['email'], l('email'), '', '', '', '', '', '', '', '', '', '');
8. find the function processing and add the following highlighted code;
$url = cleanXSS($_POST['url']);
$email = cleanXSS($_POST['email']);
$comment = $_POST['editedcomment'];
9. still within the function processing find and add the following highlighted code;
if (isset($_POST['submit_text'])) {
mysql_query("UPDATE "._PRE.'comments'." SET
name = '$name',
url = '$url',
email = '$email',
comment = '$comment',
approved = '$approved'
WHERE id = $commentid");
} else if (isset($_POST['delete_text'])) {
10. Let's add the email field to the database now.
ALTER TABLE comments ADD email varchar(255) NOT NULL;
11. Now add the gravatar class to your stylesheet.
.gravatar {
float: right;
padding: 6px;
}
12. And finally, you need to add the form text to your language file (EN.php in the lang folder most likely)
$l['cemail'] = 'Email (will not be published, for <a rel="nofollow" href="http://www.gravatar.com">gravatar</a> only)';
That should be it, you should now have gravatars available. If you don't have a default gravatar to use, here's the one I use,
![]()
As always, if I forgot a step or things didn't turn out quite right, please let me know so I can look into it and fix it if needed.



Comments
RSS Comments Feed
Matt
jesth
Scott
I've finished the new tutorial for admin styled comments using this gravatar mod. Would be cool if you could be my Guinea pig and check for any problems, no pressure though.
Heres a linky.. http://scottpayne.org/web-development/mod-admin-comments-in-snews-with-matts-gravatar-mod/
pchenk
I followed the tutorial from the beginning to make gravatar, run time does not display anything. What's wrong?? sorry my english not well..
Matt
John Flower
Matt
Did you check the database to see if the comment was actually stored? Maybe something isn't right and preventing the comment from correctly being inserted into the DB?
John Flower
Is there anywhere on the site I can make a donation? Your work is fantastic.
John Flower
Matt
That means you're probably having a problem actually inserting the comments into the DB.
Have you installed any comment mods?
Don't worry about a donation, this stuff is just for fun :)
John Flower
Thanks!
Gerry MacOstair
for the sake of less seasoned coders: in step 2.) there is a little typo, which might cause some people to search for the right portion of code in vain.
should read - it's supposed to be a concatenation rather than the begin (or end) of a string.
The mod itself works like a charm. Cheers, Gerry
Matt
I just opened a stock snews.php and you are correct.
Mine is so hacked up, I'm not sure why I even changed that, but the entire string is different, hah! I will update the article.
Gerry MacOstair
your site has a fine new design and all (I think I said some nice things about this mod already), but you might want to find a way to actually "highlight" the "following highlighted code", as time permits. Not complaining, just saying.
Cheers,
Gerry
Matt
Matt