Chat Catcher: Customize Your WordPress Comments and Trackbacks

WordPress themes are almost as varied and numerous as WordPress users. Changing the look of a blog on-the-fly is simple, and if a WordPress theme becomes a little stale, there are plenty of theme galleries to browse. But not all themes are created equal, and even if a theme is done well, WordPress upgrades can leave a theme with outdated code. If you use Chat Catcher on your blog, it’s important to understand how your theme controls the display of comments and trackbacks.  More importantly, if you’re not happy with that display, it’s important to learn how to change it.

When I wrote the Chat Catcher plugin, I felt it was important to leave as much of the existing comment system intact. WordPress manages comments effectively and I want to build on the existing code without replacing it. While this is the best approach for Chat Catcher, it does make the solution dependent on whichever theme is in place on a particular blog. For example, if a theme doesn’t display trackbacks and Chat Catcher is still set to post trackbacks, the captured tweets will never appear on the blog. Comments and trackbacks are often formatted differently, so even if a theme displays trackbacks, that doesn’t mean the Chat Catcher comments will be displayed properly.

Although there are many blog posts that address this topic, I thought it best to create one specifically for Chat Catcher. In the following sections, I’ll describe how easy it is to separate comments from trackbacks, and I’ll provide some additional tips for customizing the format of your blog’s comments.

Before You Do Anything

I highly recommend you setup a test blog before reading any further. Test blogs are wonderful things. They protect you from the embarrassment of bringing down your main blog when you make a big mistake performing a simple code change.

Now that I’ve warned you, I recognize that many of you will still apply this to your main blog without testing it first. At least make a copy of your theme files before performing any modifications. We’ll be modifying the comments.php file, so make a copy of that file in your theme folder. For example, let’s say you’re running the default theme on your blog. Go to “\{your web files}\wp-content\themes\default” and make a copy of the comments.php file.

Chat Catcher Settings

There are three main ways to leave feedback on a blog:  comments, trackbacks, and pingbacks.  All three of these are considered types of comments.  They look similar when saved in the WordPress database, but a field called comment_type is used to keep them separate.  Each regular comment has a blank comment type; trackbacks and pingbacks have a comment type of “trackback” or “pingback” respectively.

There isn’t an easy way to change a comment’s type after it has been saved. That’s why it’s important to select the comment type for Chat Catcher comments before they’re posted. To select the Chat Catcher comment type, navigate to the Chat Catcher Settings page and select from the list. If your blog receives a moderate amount of regular comments, I recommend separating your Chat Catcher comments so that they do not appear inline with regular comments. Use the Trackback or Custom Trackback (available in Chat Catcher ver. 2.75) type to separate the Chat Catcher comments. It’s important to note that you may need to modify your theme depending on the choice you make. Your readers, however, will appreciate the effort.

cccommenttype

 

 

Modifying the Comment Display

Open the file called comments.php in your current theme. Comments are often displayed using a “loop” with a “foreach” statement. An example is shown below:

<ol id="commentlist">
<?php foreach ($comments as $comment) : ?>
	<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
	<?php echo get_avatar( $comment, 32 ); ?>
	<?php comment_text() ?>
	<p><cite><?php comment_type(_c('Comment|noun'), __('Trackback'), __('Pingback')); ?> <?php _e('by'); ?> <?php comment_author_link() ?> &mdash; <?php comment_date() ?> @ <a href="#comment-<?php comment_ID() ?>"><?php comment_time() ?></a></cite> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
	</li>
<?php endforeach; ?>
</ol>

In Version 2.7 of WordPress, a function was added to display the comments without a loop. Although the function is simpler to code, it does make it more difficult to customize the comment layout.

Using wp_list_comments():

	<ol class="commentlist">
	<?php wp_list_comments(); ?>
	</ol>

Now that you’ve seen the standard code, it’s time to replace it. Let’s copy the delivered code and change it to separate different comment types. Keep in mind that your existing comments.php may have special formatting or the code may already be set to separate the comments and trackbacks.  Make sure you have a backup of comments.php so that you can recreate the original and incorporate theme-specific coding if necessary.

The code displayed below is from a comment loop, modified to separate comments and trackbacks:

<ol id="commentlist">
<?php
      $wp_query->comments_by_type = &separate_comments($comments);
      $comments_by_type = &$wp_query->comments_by_type;
?>
<?php foreach ($comments as $comment) : ?>
<?php if($comment->comment_type == '') : ?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
	<?php echo get_avatar( $comment, 32 ); ?>
	<?php comment_text() ?>
	<p><cite><?php comment_type(_c('Comment|noun'), __('Trackback'), __('Pingback')); ?> <?php _e('by'); ?> <?php comment_author_link() ?> &mdash; <?php comment_date() ?> @ <a href="#comment-<?php comment_ID() ?>"><?php comment_time() ?></a></cite> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
	</li>
<?php endif; ?>
<?php endforeach; ?>
</ol>
<ol class="commentlist">
<?php if(count($comments_by_type['trackback']) > 0 || count($comments_by_type['pingback']) > 0) : ?>
<h3>Trackbacks</h3>
<?php endif; ?>
<?php foreach ($comments_by_type['trackback'] as $comment) : ?>
	<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
	<?php echo get_avatar( $comment, 32 ); ?>
	<?php comment_text() ?>
	<p><cite><?php comment_type(_c('Comment|noun'), __('Trackback'), __('Pingback')); ?> <?php _e('by'); ?> <?php comment_author_link() ?> &mdash; <?php comment_date() ?> @ <a href="#comment-<?php comment_ID() ?>"><?php comment_time() ?></a></cite> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
	</li>
<?php endforeach; ?>
</ol>

This example shows how to separate comments and trackbacks using the wp_list_comments() function:

 	<ol class="commentlist">
	<?php wp_list_comments('type=comment'); ?>
	</ol> 

      <?php
      $wp_query->comments_by_type = &separate_comments($comments);
      $comments_by_type = &$wp_query->comments_by_type;
      if(count($comments_by_type['trackback']) > 0 || count($comments_by_type['pingback']) > 0)
      {
          echo '<h3>Trackbacks</h3>';
      }
      ?>
      <ol class="commentlist">
	<?php wp_list_comments('type=trackback'); ?>
      </ol>

Save the comments.php file and view a blog post. The comments and trackbacks/pingbacks should be neatly separated.

Separating Chat Catcher Comments

If you selected “Trackback” on the Chat Catcher settings page, the Chat Catcher comments will display under the Trackbacks heading. If you would like to further separate Chat Catcher comments, you can select the “Custom Trackback” type and modify your theme.

Modifying your theme for Custom Trackbacks requires the same type of coding as was used to separate regular trackbacks. Instead of specifying ‘trackback’ in the code, you’ll use a custom type called ‘ctrackback’.

For the loop-style comment display, simply copy the trackback section and change the comment types. Change $comments_by_type['trackback'] to $comments_by_type['ctrackback']:

<ol class="commentlist">
<?php if(count($comments_by_type['ctrackback']) > 0 ) : ?>
<h3>Social Media Comments</h3>
<?php endif; ?>
<?php foreach ($comments_by_type['ctrackback'] as $comment) : ?>
	<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
	<?php echo get_avatar( $comment, 32 ); ?>
	<?php comment_text() ?>
	<p><cite><?php comment_type(_c('Comment|noun'), __('Trackback'), __('Pingback')); ?> <?php _e('by'); ?> <?php comment_author_link() ?> &mdash; <?php comment_date() ?> @ <a href="#comment-<?php comment_ID() ?>"><?php comment_time() ?></a></cite> <?php edit_comment_link(__("Edit This"), ' |'); ?></p>
	</li>
<?php endforeach; ?>
</ol>

 

If you are using the wp_list_comments() function, copy the code and change the comment type. Change $comments_by_type['trackback'] to $comments_by_type['ctrackback'] and change ‘type=trackback’ to ‘type=ctrackback’:

 <?php
      if(count($comments_by_type['ctrackback']) > 0)
      {
          echo '<h3>Social Media Comments</h3>';
      }
?>
<ol class="commentlist">

<?php wp_list_comments('type=ctrackback'); ?>

</ol>

 

As you’ve seen, it’s very easy to modify the comment section of WordPress through your theme.  Although you usually won’t be forced to make these changes, it’s a good idea to at least consider them.  If you don’t feel comfortable making these changes, perhaps the best way to handle this is to ask a friend for help.  As a last resort, you can always search for a new theme with better comment handling.

 

 

  • Share/Bookmark

13 Comments so far »

  1. Aneslin said,

    Wrote on June 29, 2009 @ 10:58 am

    thx bro, this is the one excatly wat i need, I search for this for last 2 days. but i cudnt find any proper answer, but today I got ur link via twitter.

    I just need to display the trackback url in my template. thats all.

    is there any plugin for this particular work?
    then I dont need to play with template php codes :)

    thx in advance.

  2. Shannon Whitley said,

    Wrote on June 29, 2009 @ 11:18 am

    @Aneslin

    This page has the code to display the trackback url:

    http://codex.wordpress.org/Template_Tags/trackback_url

    I’m not aware of a robust plugin that will handle this for you.

  3. Ari Herzog said,

    Wrote on July 2, 2009 @ 1:43 am

    It’s unclear what coding you suggest be added/edited for the custom trackback section. Can you use a different color for this?

  4. swhitley said,

    Wrote on July 3, 2009 @ 9:38 pm

    Hi @Ari

    I’m looking for a method that would allow me to control the highlighting in the code window. For now, I’ve pulled the code out above the paragraph. It’s mostly a matter of just adding a “c” to the beginning of each occurrence of “trackback.”

  5. swhitley (Shannon Whitley) said,

    Wrote on July 15, 2009 @ 10:33 am

    Twitter Comment


    @codepo8 Hi, you might want to take a look at this to help with your Chat Catcher display: [link to post]

    Posted using Chat Catcher

  6. swhitley (Shannon Whitley) said,

    Wrote on July 15, 2009 @ 11:34 am

    Twitter Comment


    @awolk Hi, I noticed your comment on Chat Catcher. Comment display depends on your theme. – [link to post]

    Posted using Chat Catcher

  7. Shannon Whitley said,

    Wrote on September 1, 2009 @ 7:57 am

    By the way, I don’t separate the comments and trackbacks on this blog. I simply don’t get that many of either. :)

  8. dialupkid (Niels vT) said,

    Wrote on October 25, 2009 @ 3:57 pm

    Twitter Comment


    @brewbart hier nog wat extra info [link to post]

    Posted using Chat Catcher

  9. Streaming said,

    Wrote on December 4, 2009 @ 6:12 pm

    thank you for the tips

  10. TopGear said,

    Wrote on December 6, 2009 @ 7:27 pm

    lol what? I didn’t get that :D

  11. Alexandre Affonso said,

    Wrote on January 7, 2010 @ 4:59 pm

    Thanks a lot.

    I have seen thousand of tutorials but no one with wp_list_comments explanation.

    I’m also going to have a look on Chat Catcher, seems interesting.

  12. Infographiste said,

    Wrote on January 30, 2010 @ 11:22 am

    Hey Shannon, thank you for the tips, i’m new with wordpress and find your tuto very useful, page bookmarked, have a great week end :)

  13. shulato said,

    Wrote on June 16, 2010 @ 6:38 pm

    this is what i’m looking for! thanks! web development

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment:

Twitter Users!
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.