scriptygoddess

21 Jun, 2004

Comment Highlighting (based on author)

Posted by: Jennifer In: WordPress scripts

Monty asked about comment highlighting based on author.

Even though this isn't a plugin, I thought this could work similar to the way I was doing comment highlighting

Again, after your comment loop:

<?php foreach ($comments as $comment) { ?>

Add the following (with edits as neccessary) :

<?php
$isByAuthor = false;
if($comment->comment_author_email == 'YOUR-EMAIL') {
$isByAuthor = true;
}
?>

Then before your comment text begins (with <?php comment_text() ?>) add this:

<?php if($isByAuthor ) { echo '<div class="authorcomment">';} ?>

To close the DIV tag – BEFORE the ending LI tag for the comment add this

<?php if($isByAuthor ) { echo '</div>';} ?>

Then add a style "authorcomment" to your style sheet. Style however you like.

Then as long as you use that email when you comment – the comment will be highlighted.

See alternate (simplified) variations in the comments

34 Responses to "Comment Highlighting (based on author)"

1 | TestBlog » Comment Author Hilite

September 25th, 2004 at 8:28 pm

Avatar

[…] 8217;t know if there are any plugins already out there that do this – all I could find was a method which involved editing the actual code at Script […]

3 | ***masuga.com » Shiny New Comments | superfine web design

December 10th, 2004 at 3:27 am

Avatar

[…] everybody else's. The code used for the author comment differentiation was found at Scriptygoddess. If you're a WordPress us […]

4 | davidhay » Blog Archive » Customizing Wordpress: Reloaded

December 13th, 2004 at 2:18 pm

Avatar

[…] ;customising WordPress". For that, I apologise. This post is abysmal – for example: […]

5 | Code Novice

June 22nd, 2004 at 2:32 am

Avatar

WP Scripts and Plugins
I mentioned Jenn's WordPress additions on Scriptygoddess yesterday, but she has several standouts in her subcategories that are definitely worthy of a mention on their own. A sampling of her recent efforts:

6 | Mark J

June 22nd, 2004 at 3:17 am

Avatar

Because the e-mail field is never displayed publicly, it can be used as a password of sort. A poor man's authentication, if you will. I use this to add a little badge next to comments I make on my own site (did this with Movable Type as well). A little PHP logic checks to see if the e-mail address matches my secret "password" e-mail address, and if it does, it knows it is me. This is a good way to avoid confusion when you only use your first name to post comments, or when some clown thinks they can impersonate you on your own site.

7 | Jennifer

June 22nd, 2004 at 8:22 am

Avatar

Actually – you're right – I guess it could be simplified to just check email… Can you see any other reason to have it check all three? (if not, I'll update the post)

8 | Mark J

June 22nd, 2004 at 8:35 pm

Avatar

Can't think of a reason to have it check all three, especially since the other two (URI and Name) are publicly visible. Heck, I can think of a good reason NOT to have it check all three. Say you're at a library where your comment posting cookie isn't saved, and you put down your URL without the trailing slash, and that causes the if() logic to fail.

9 | Jennifer

June 22nd, 2004 at 9:03 pm

Avatar

Very true. Post has been updated.

10 | Monty

June 28th, 2004 at 1:20 am

Avatar

Thank you so much Jennifer

11 | Mark J

July 3rd, 2004 at 8:50 am

Avatar

Here's the code I use to add a badge next to comments I leave on my own blog.

You basically just make up a fake secret e-mail address and use that to post comments:

<?php
if($comment->comment_author_email == 'my_secret_address@mydomain.com'){ ?>
<span class="blogauthor">
<?php } else { ?>
<span class="comment-author">
<?php } ?>
<?php comment_author_link(); ?></span>

Then all you have to do is style "blogauthor" and "comment-author" differently. To add a badge, you would do something like this in your CSS:

.blogauthor { background-image:url(http://www.domain.com/images/blog_author.gif);
background-position:right;
background-repeat:no-repeat;
padding-right:80px;
}

12 | Jennifer

July 4th, 2004 at 7:50 am

Avatar

My code for this is slightly different because I'm not using classes, etc. – and I've actually simplified it from the original post…
(Code in italic already exists)

<?php comment_author_link() ?>
<?php if ($comment->comment_author_email == 'MyEmailAddress@example.com') {
echo '<img src="/images/author.gif" width="64" height="9">';
} ?>
<?php comment_date() ?>

13 | Carla

July 7th, 2004 at 7:23 pm

Avatar

How did you modify your badge code, Jennifer, to include the subscriber badge as well?

14 | Jennifer

July 7th, 2004 at 7:29 pm

Avatar

The code for both is here.

15 | Mama Write

July 7th, 2004 at 8:01 pm

Avatar

Various WordPress Links
Rather than ping ScriptyGoddess multiple times and clog my sideblog with even more WordPress links, I decided to plop 'em all here. I'm going to give WordPress a try again on my course website, since there are various hacks that…

16 | Carla

July 9th, 2004 at 8:38 am

Avatar

Thank you! You are so punk rock. :)

17 | Poonam

July 12th, 2004 at 10:02 am

Avatar

I am utilizing the Comment Highlighting and I am also using your Drop-Down Comments. But it only highlights my comments when someone clicks on the "Add Comment" page. If you go to "Show Comment"…my comments (as the author) does not get highlighted. Do I have to add this code to my dropdown-comments.php for it to work when someone clicks on "Show Comments"

18 | Jennifer

July 14th, 2004 at 7:52 am

Avatar

Poonam – yes – you need to add the code to every "comment" page you're using.

19 | Poonam

July 14th, 2004 at 8:08 pm

Avatar

Thanks Jennifer for the info!

20 | Poonam

August 11th, 2004 at 2:55 pm

Avatar

One more question Jennifer – are you able to manipulate this code so that if you have multiple authors (two) for one blog..then they can have different colors.

For instance, I would have color silver and my husband would have yellow color.

Thanks!

21 | Jennifer

August 12th, 2004 at 8:45 pm

Avatar

Poonam- try this:

<?php
if ($comment->comment_author_email == 'author1email@example.com') {
echo '<img src="/images/author1.gif" width="64" height="9">';
} else if ($comment->comment_author_email == 'author2email@example.com') {
echo '<img src="/images/author2.gif" width="64" height="9">';
}
?>

etc. etc. (just keeping adding else ifs for as many authors/author email addresses there are)

22 | Poonam

August 13th, 2004 at 12:37 am

Avatar

Thanks Jennifer! I have some questions.

So does this go along with the code that you have shown..or do i take all of that out and put this in?

Then in my css, I have so do I keep that..or no?

Lastly, what's the image source for images/author2.gif? Do i need to add something to my images folder?

Thank you in advance. :smile:

23 | Kirrin Jones

September 26th, 2004 at 4:00 pm

Avatar

Is there an actually plugin/hack for this with instructions that I may add to my website?

If there is, could it be e-mailed to me?

Thanks.

24 | Jennifer

September 26th, 2004 at 9:44 pm

Avatar

Kirrin – This post recently pinged mine saying they had created a plugin (rather than edit code like my post instructed). I haven't tried it, so I can't speak to it, but maybe that's what you were looking for?

25 | Tom

October 6th, 2004 at 11:39 am

Avatar

I just used this technique on my site which works great – however does not prevent spoofing. Someone mentioned that the e-mail address is not shown, but it is (even in this page, I see the email field) so I'm not sure what is being referred to. If I am showing the e-mail field – I can't prevent someone from filling in my information and spoofing me.

Is there some way I can restrict it to a certain IP address or some other technique?

Thanks!

Tom

26 | Jennifer

October 6th, 2004 at 11:50 am

Avatar

Tom – the point is that someone who doesn't already know your email address can not derive it from the page shown to them. While the FIELD is there for you to fill out, the data is not relayed back on the page. And even if they do know your address – you can use a seperate "special" one just for when you fill out comments on your own blog (sort of like a "password").

27 | Tom

October 6th, 2004 at 11:54 am

Avatar

Oh! Good heavens I kept thinking of the form itself – not the display of the comments. Gee is my face red! :) So I get you, I can use one of my Yahoo! AddressGuard emails for example, that nobody knows, so nobody would ever be able to spoof me (barring they don't guess it) correct?

Thanks!

28 | Scott Vandehey

March 12th, 2005 at 5:53 pm

Avatar

I have a version that I modified for use on my site, where I have several authors posting, and I want each comments page to highlight the comments from the author of the post without hard-coding any email addresses:

<?php
global $authordata;
$authorcomment = false;
if($comment->comment_author_email == $authordata->user_email) {
$authorcomment = ' authorcomment';
}
?>

I hope this helps someone. I'm not a PHP guy at all, and it took beating my head against the wall for several hours to figure this out. Any suggestions, please let me know!

29 | Scott Vandehey

March 14th, 2005 at 2:49 pm

Avatar

Improved version based on some feedback from the WP support forums:

<?php
$authorcomment = ";
if($comment->comment_author_email == get_the_author_email()) {
$authorcomment = ' authorcomment';
}
?>

30 | Christopher

March 15th, 2005 at 7:04 pm

Avatar

Ok, wordpress newbie here, does this work in wp 1.5, I did a quick search thru the comments pages I have in my templates and didn't find < ?php foreach ($comments as $comment) { ?>, I'm wondering if this changed in wp 1.5 hmm..

31 | Christopher

March 15th, 2005 at 7:14 pm

Avatar

Ok, please disregard my previous comment, but now I have an issue with using this with two authors, how do I apply seperate styles for different authors..?

32 | Golfy

April 9th, 2005 at 6:33 pm

Avatar

This is great! I tried to use the plugin but it didn't work for me. Modifying a few lines of according to yor tutorial is so easy and works perrrfect!

Thanks again :)

33 | Marya

April 16th, 2005 at 1:36 pm

Avatar

I'm trying to use the code at my comments, but I'm having a little trouble. For example, the comments from my visitors have a yellow background, and I'd like mine to have a pink background… So when I copied the code, my comments came within a yellow cell (the default comments style) and then a pink cell (my comments). How can I separate my comments style to the others?

34 | Christopher

April 19th, 2005 at 10:59 pm

Avatar

You might want to check out my site, I used this script to do some pretty cool stuff.. I think what you asking for.

http://www.chrisvschris.com

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements