Bob's Hobby Bob's Hobby blog

Customize to display trackback URL in WordPress comments

I have an email from someone to ask me about how to display/separate trackback url like the comment in my posts.Here is the trackback url example that I have customized to display in the comment in my posts by separating the trackback out of real comments from real visitors and group them on the top of real posts.In order to customize your trackback URL style you need to edit 2 files in wordpress source code.
1. comments.php locate in wp-content/themes/YourTheme/comments.php
2. comment-template.php locate in wp-includes/comment-template.php

Edit comments.php
First look for the following line inside comments.php

<ol class="commentlist">

Before the above line paste the following code:

<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type != 'comment') { ?>
<li id="comment-<?php comment_ID() ?>">
<cite><?php comment_author_link() ?></cite><small> on <?php comment_date('d M Y') ?> at <?php comment_time() ?></small><br/>
<?php if ($comment->comment_approved == '0') : ?>
<em>Your comment is awaiting moderation.</em><br />
<?php endif; ?>
</li>
<?php } ?>
<?php endforeach; ?>

Next look for the following line

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

Before the above line paste the following code:

<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>

Next look for the following line

<?php endforeach; /* end for each comment */ ?>

Before the above line paste the following code:

<?php } ?>

After edit file your code look like this

//display trackbacks loop
<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type != 'comment') { ?>
.
.
.
<?php endforeach; ?> // End foreach trackbacks loop
// display comments loop
<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?> 
.
.
.
<?php endforeach; ?> // End foreach comments loop

In the code above, the trackbacks list will be shown before comments list in your post/page.You can place the trackbacks loop after comments loop.

Edit comment-template.php
Look for the following line (around line 45 in function get_comment_author_link() )

if ( empty( $url ) || 'http://' == $url )

Before the above line paste the following code:

if (get_comment_type()=='comment'){

Next look for the following line around line 49

return apply_filters('get_comment_author_link', $return);

After the above line paste the following code:

}else{
if ( empty( $url ) || 'http://' == $url )
$return = $author;
else
$ac = parse_url($url);
$return = "Trackback from : <a href='$url'
rel='external nofollow' class='comment' target='_blank'>".$ac['host']."</a>";
return apply_filters('get_comment_author_link', $return);
}

Finally,save and upload both files to your server.