Using hCard in Wordpress comments

by Colin Devroe on April 20th, 2006

A technical article, from me? There are some very good reasons I stopped releasing code to the world, but I think I have very good reasons I am going to start up again.

Today, we’re going to be learning how to format our comments, for Wordpress, to work within the hCard spec. If you are unaware what microformats are, what hCard is, or why you might consider doing this on your site, please take a few minutes to read the microformats site.

As a side note: Even if you do not see yourself trying to directly benefit from using hCard in your comments, others will. Adding hCard syntax to your existing comments on Wordpress is painless, and should not effect your layout at all. So do it anyway!

Current Wordpress comment markup

Obviously, this markup depends on the theme you are using, but I’m going to show you the most common example, and you can hopefully adapt it to your current theme.

The following is the markup found in Michael Heilemann and Chris J. Davis’Kubrick, which comes pre-installed with every copy of Wordpress and is, by default, chosen as your theme. This theme has been customized thousands of times, and so most of you out there should be familiar with this markup.

<li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
	<cite><?php comment_author_link() ?></cite> Says:
	<?php if ($comment->comment_approved == '0') : ?>
	<em>Your comment is awaiting moderation.</em>
	<?php endif; ?>
	<br />

	<small class="commentmetadata">
		<a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a> <?php edit_comment_link('e','',''); ?>
	</small>

	<?php comment_text() ?>

</li>

This markup is replicated, by Wordpress, for every comment you have on each post. So, how do we make this hCard compatible?

There are a few essential elements to a hCard. The first, is marking up where the actual hCard is. In this case, we’re going to use the cite tag since that is where we have the commentor’s first name, last name, and url to their website. The second essential element would be the annotation of the commentor’s formatted name. To do this, we need to add 3 class names to your comments.

  • vcard: This signifies the beginning of an hCard
  • fn: This signifies the formatted name (or the way that the user would like their name shown).
  • url: The home url for the commentor.

There has been some discussions in the world of microformats recently about being able to uniquely target single hCards on a page with multiple hCards, hence I am also going to add an ID to each hCard, so that if something takes off in this area, your comments will already be compatible. So, below is the code that you may use inside of Kubrick v1.5 for your commentor’s information to be marked up as hCards.

<li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
			<cite id="vcard-<?php comment_ID() ?>" class="vcard"><a class="url fn" href="<?php comment_author_url(); ?>"><?php comment_author(); ?></a></cite> Says:
			<?php if ($comment->comment_approved == '0') : ?>
			<em>Your comment is awaiting moderation.</em>
			<?php endif; ?>
			<br />

			<small class="commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a> <?php edit_comment_link('e','',''); ?></small>

			<?php comment_text() ?>

		</li>

It really is just that easy. Again, adding these few classes should not effect your layout, and can be installed painlessly into your site, for your benefit and ours.

Digg this article to help others find it.

[tags]wordpress, kubrick, microformats, hcard, xhtml, semantics[/tags]

Tags: , , , , ,

12 Responses to “Using hCard in Wordpress comments”

  1. Excellent stuff Colin. I will give this a go on DennisBullock.com. BTW, I digg!

  2. Dennis: Cool, thanks. I look forward to seeing your implementation.

  3. To be honest I’m not really sure how this is beneficial, but I implemented it into my comments. Time to do some research.

  4. [...] I’ve been using ClaimID a lot more lately. Colin has also put together a bit of info about using hCard in Wordpress Comments. [...]

  5. Please note that this markup will only produce meaningful hCards in the cases where the Implied-N-Optimization applies. See Drew’s writeup for more details.

  6. [...] support for hcards in comments (hack) [...]

  7. Well I just started a blog yesterday and I am still getting the hang of Wordpress and the admin panel but once I do, I will give your h card advice a try. I’ll let you know how it turns out.

  8. [...] Devroe’s instructions on how to add hcard to Wordpress comments. While there may be problems with the implementation it’s a good first [...]

  9. [...] Using hCard in Wordpress comments | Colin Devroe [...]

  10. Hey Colin, thank you! great job. Greetings, Martin from http://www.ausmusterung.biz

  11. [...] Devroe’s instructions on how to add hcard to Wordpress comments. While there may be problems with the implementation it’s a good first [...]

  12. Why would anyone want this?.. I cannot see why name and a URI would be that useful.

Leave a Reply

About Colin

Colin and Eliza Devroe

Hi, I'm Colin Devroe the Technology Evangelist for Viddler. I enjoy writing, photography, wine, and food. (more)

Categories

Archives