Ever notice how some WordPress blogs make it a little more obvious when the author comments on his own posts? For some crazy reason, that’s not a standard feature in WordPress, but can easily be done with a little PHP and CSS.

There’s probably already a WordPress plug-in that does this, but some things are so easy to do that I just change the code in the theme editor. That’s the case with highlighting your own comments on your WordPress blog for a more official look and to keep jackasses from pretending to be you.

This method checks against the user ID, rather than an email address… which could be guessed.

If you’re already lost, just look at the first two comments from me at the bottom of this post, one of which I’ll logout for.

It’s easy enough to do, as it only requires one little code insert in comments.php and a CSS class defined in style.css.

Note: If you’re going to copy and paste the code, be sure to delete the space between the opening bracket and the question mark, which is added here by the code highlighter.

First, we’ll add the PHP code. Login to your WordPress admin panel, then goto Design -> Theme Editor and click on “Comment (comments.php)” to edit it. Scroll down and find the line that starts out like this:

	<li class="&lt;?php echo $oddcomment; ?&gt;"></li>

Before that line, add this:

&lt; ?php
if ($comment-&gt;user_id == 1) {
   $oddcomment = 'itsme';
}
?&gt;

In most cases, that’s all you’ll need to do, so you can go ahead and save the file. That code will always highlight comments from user ID 1.

Maybe you have several users that create posts and you’d always like their comments to stand out on the posts they wrote. In that case, you can change the above code to this:

&lt; ?php
if ($post-&gt;post_author == $comment-&gt;user_id) {
   $oddcomment = 'itsme';
}
?&gt;

I’ll add a few more options and tips at the bottom for those that want to do a little more, or highlight multiple users.

Once you’ve saved your changes in comments.php, go ahead and click on “Stylesheet (style.css)” to edit it. You can add this right at the top and edit it to your liking:

.itsme {
background:#dadace;
border: 1px dashed #838270;
}

Depending on your theme, simply defining the class like that might not work. If not, it’s easy enough to fix. Just scroll down and find where “.alt” class is defined. If there’s anything before that, you’ll probably have to put the same thing before .itsme. In my case, with the Fleur theme, my entry looks like this instead:

#rap .commentlist .itsme {
background:#dadace;
border: 1px dashed #838270;
}

Once you’ve got it, you can change whatever you want in the .itsme class, but I kept it simple with just a border and background color.

In most cases, that’s probably all that needs to be done. Enjoy!

In other cases, you might run into one of these problems:

  1. Your user ID isn’t 1.
  2. You want to highlight more than one ID.
  3. You want to highlight more than one ID, each with their own class.
  4. You have no idea what the user ID is or how to find it.

Let’s start with the easy one, #1. If you simply want to hightlight a different user ID, simply change the 1 in the if statement to whatever you’d like.

Number 2 can be done a few different ways. The first example just checks for whichever ID numbers you specify, separated by ||.

&lt; ?php
if ($comment-&gt;user_id == 1 || $comment-&gt;user_id == 2) {
   $oddcomment = 'itsme';
}
?&gt;

Or maybe you want a range of IDs to be highlighted, using 2 through 4 in this example:

&lt; ?php
if ($comment-&gt;user_id &gt; 1 &amp;&amp; $comment-&gt;user_id &lt; 5) {
   $oddcomment = 'itsme';
}
?&gt;

Or you could keep an array of user IDs:

&lt; ?php
$highlight_these = array(1,2,3,4,5);
if (in_array($comment-&gt;user_id, $highlight_these)) {
   $oddcomment = 'itsme';
}
?&gt;

If you want to go a little more advanced than that, you could assign each of the user IDs a different class. You would just need to add those classes to your stylesheet (style.css) like you did with the first one.

&lt; ?php
$highlight_these = array(
   1 =&gt; 'user1',
   4 =&gt; 'user4',
   7 =&gt; 'user7'
);
 
if (array_key_exists($comment-&gt;user_id, $highlight_these)) {
   $oddcomment = $highlight_these[$comment-&gt;user_id];
}
?&gt;

In that example, several users could use the same class, though I stuck with a naming convention that linked them to the user ID.

Those should pretty much cover the basics. I just use the first simple example on this site, since I only care to highlight my own comments.

If you don’t know what user IDs to use, there are two easy ways to find them. One would be to simply browse the wp_users table in your database. Or, if you’re already playing around with your theme editor, you can temporarily add this code to comments.php (same place where you put the original code) to spit out the user IDs in HTML comments:

&lt; ?php
echo '<!-- User ID: ' . $comment->user_id . ' -->';
?&gt;

Then, just go view one of your blog posts that has comments, view the source and scan for the user ID comments to see the ID number.

Like I said, there are probably WordPress plug-ins out there that already do this, so this is more for people that like playing with the code themselves.

One Other Note: I had one WordPress blog that this didn’t work on, that had previously used a threaded comment plugin that used a different comments.php file. Going back to the default file fixed that problem.