Gravatar mod for sNews 1.7

Comments (28)

gravatarsWith 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.

bookmark / share this: Bookmark and Share
rated 4.67/5 (6 votes)


28 comments

Add a new comment »

Patric Patric said:
Mar 7th, 2009 at 9:53 am

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 ?


Matt Matt said:
Mar 7th, 2009 at 10:00 am

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.


Patric Patric said:
Mar 9th, 2009 at 1:31 am

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


Matt Matt said:
Mar 9th, 2009 at 4:44 pm

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


asundrus asundrus said:
Apr 28th, 2009 at 2:46 pm

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.


asundrus asundrus said:
Apr 28th, 2009 at 3:03 pm

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


Matt Matt said:
Apr 28th, 2009 at 3:59 pm

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


asundrus asundrus said:
Apr 28th, 2009 at 4:12 pm

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


Matt Matt said:
Apr 28th, 2009 at 4:21 pm

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.


asundrus asundrus said:
Apr 28th, 2009 at 5:44 pm

Message shot :)


Matt Matt said:
Apr 28th, 2009 at 7:00 pm

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.


Deyn Deyn said:
Aug 6th, 2009 at 1:23 am

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


Adhi Adhi said:
Aug 27th, 2009 at 3:43 am

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.


Matt Matt said:
Aug 27th, 2009 at 7:35 am

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;


Kreatur Kreatur said:
Oct 4th, 2009 at 9:27 am

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 :)


Matt Matt said:
Oct 4th, 2009 at 9:37 am

Hi Kreatur,

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

.clearer {
clear: both
}


Kreatur Kreatur said:
Oct 4th, 2009 at 12:56 pm

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


Matt Matt said:
Oct 5th, 2009 at 7:02 am

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!


Sven - Philippe Sven - Philippe said:
Nov 11th, 2009 at 3:25 am

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


Matt Matt said:
Nov 11th, 2009 at 9:45 am

Yeah, I briefly discussed it here; http://mdj.us/snews-cms/hacks-mods/pretty-date-and-comments-bars-in-snews-cms/#Comment21

Maybe I could do it as separate mod, but I know my "infoline" is hacked up, so it may not be as easy for the average user.


jesth jesth said:
Mar 8th, 2010 at 9:42 pm

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.


Matt Matt said:
Mar 9th, 2010 at 6:54 am

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


jesth jesth said:
Mar 9th, 2010 at 8:51 pm

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.


Matt Matt said:
Mar 10th, 2010 at 7:15 pm

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.


jesth jesth said:
Mar 10th, 2010 at 7:43 pm

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.


Matt Matt said:
Mar 10th, 2010 at 7:51 pm

jesth, I'll shoot ya an email tomorrow and see if I can explain what I did.


jesth jesth said:
Mar 10th, 2010 at 8:00 pm

That would be really, cool, atleast then I will know if I ever where close to actually doin the right thing.


Scott Scott said:
Apr 8th, 2010 at 5:49 pm

Hey Matt,

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/



Write a comment

* = required field

:

:

:

:

You may insert urls in plain text, urls will be automatically linkified for trusted users and on seasoned posts only. All first comments are moderated, so use your email if you want to be remembered.


Back to top