Customising WordPress comments

How to customise the HTML markup for WordPress comments

I wanted to make some changes to the code WordPress spits out for comments. This included:

  • changing the date format
  • removing comment author links
  • removing the class ‘vcard’ (it’s an old structured data format)

Your reasons for changing the WordPress comments format will probably be different to mine but the solution is still the same. Hopefully this quick tutorial will help.

How to customise comments

Open the comments.php file in your theme and replace:




This will replace the default function for displaying comments. Go to your functions.php file and add the following:

// Custom comments
function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
     <div id="comment-<?php comment_ID(); ?>">
	      <div class="comment-author">
	      	<?php echo get_avatar( $comment, 56 ); ?>
	        <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author()) ?>
	      <div class="comment-moderation">
		      <?php if ($comment->comment_approved == '0') : ?>
		         <p><?php _e('Your comment is awaiting moderation.') ?></p>
		      <?php endif; ?>
		  <div class="comment-meta commentmetadata">
	      	<p ><?php printf(__('%1$s at %2$s'), get_comment_date('j F, Y'),  get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?></p>
		  <div class="user-comment">
	      	<?php comment_text() ?>
	      <div class="reply">
	         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
<?php } 

Note the lack of a trailing </li>. In order to accommodate nested replies, WordPress will add the appropriate closing tag after listing any child elements so there is no need to add one.

You can find more info on the comments callback function from the WordPress codex website. I also found this older post on Deluxe Blog Tips incredibly useful.

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts