Gravatar mod for sNews 1.7

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,
default gravatar

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.

Tags

 

You might like

Comments


Yo, Matt... added all things, but the database insertion seems tp feck things up. This do not work for me, gets an error in MySQL... I've emailed you, could you find a minute for me ?


Patric,

Looks like I have a comma in the ALTER TABLE syntax that shouldn't be there, right before the closing semi-colon.

I'll update that and get you on email/IM.


Well, all things as they should be, and we're up. Thanks for all help.


Not a problem, the gravatars look great on your site too, Patric.


I followed the instruction, but for some reason, it doesn't show avatars, so I looked at the testcomments I made, and saw that the email I entered somehow disappeared, which ofcourse then is the reason for the avatar not to show.
So I tried adding the email via edit, and then it worked, what could I have missed? i'm certain that I did everything 100% correct, I tripple checked.


Back, I guess tripple checking aint helping if one is too tired hehe, sorry, I didnt add step 2:
$query = "INSERT INTO "._PRE."comments(articleid, name, url, comment, time, approved, email) VALUES('$post_article_id', '$name', '$url', '$comment', '$time', '$approved', '$email')";



But now I did and ofcourse it's working now.

/me feels a bit embarrassed


lol, no worries man, glad it's working for you, let me know if you get stuck on anything.


All I need is help with the admin-comment styling.


Trying to figure out how to wrap comments in a div?

Asundrus, shoot me a message via the contact form and I'll follow up with you on email later tonight or tomorrow morning.


Message shot :)


Sweet, shot ya back... let me know if what I sent works for you, I will try to do a proper tutorial on it at some point.


ahh, i like this mod, it's very usefull
thx matt


Hi, when I try this mod, the gravatar images cross the comment area's bottom line when the comment is just short (for example, the comment just has 1 line).

It doesn't appear just like yours, in this blog, that the gravatar images stay still inside the comment area, no matter how long it is.

Can you help me? Maybe I just need to fix the style.css, but I don't know how to be fix it.

Thanks a lot.


Hi Adhi,

Do you have a link, or can you take a screen shot and post it? It definitely sounds like a styling issue.

From the sound of it, is it over-lapping the admin links or other content? If that's the case, perhaps adding a clearer break after the comment would fix it?

i.e. add the break in step four

echo $r['comment'];
echo '<br class="clearer" />';
break;


Hello, i have also sent an e-mail, but maybe you missed it, i have the same problem as Adhi, but it's not because of the break line. please take a look at my website when you can, cause right now my gravtars ar flying all over the place :)


Hi Kreatur,

Make sure the break clears the floats, hence the class "clearer". The css would be

.clearer {
clear: both
}


ty, didn't cross my mind to try and manipulate the clearer class. everything works great now.


That's understandable Kreatur, the "clearer" class is included within the default sNews style, but seeing as how you have your own design there, it makes sense that you wouldn't have it.

BTW, Nice looking site!


An idea sheriff: would it be a good idea to mix that Mod with the admin comments you seem to use?
I would be candidate for testing. -D


I really like this mod, I just have one thing, how the heck do I move the placement of the gravatar pic?
I mean, the way you have it is cool, left to the date, I tried a few things, but gave up, maybe I'm too tired, don't know.
So if you get the time, and want to share, please do.


Hi jesth, everything is controlled through the CSS styling really, though I have hacked around a bit in the comments function in snews.php.


Iv'e tried "hacking" around in the comment function, and messing up my css, but still can't get my wanted placement, but once again, it's the middle of the night, fresh eyes might help.


Yeah, it took me a while actually, my comment function is completely butchered and was done so long go, i can't really remember everything I did :P.

If you have an idea of what you want and enough PHP & CSS under your belt, it shouldn't be too difficult with some trial and error.


I tried all sorts of things, but can't move that darn gravatar outside the comment area, will try again, I must be missing something simple I guess.


Comments are closed. No new comments allowed.

Copyleft 2002 - 2017 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top