Separate Pingback/Trackback from Comments

Seperating Pingbacks and Trackbacks from WordPress Comments

Unfortunately, WordPress does not come with a built in function to separate pingbacks, trackbacks from comments. Personally, I find it annoying to be reading comments and then seeing the pingback and trackback links in the same comment area. It just didn’t look right.

If you want to completely disable pingback and trackback from your blog. Use this WordPress plugin called Comment Sorter. There are other ways to go disable your pingback and trackback without a plugin, check it out here. However, I don’t see why would anyone want disable their pingback and trackback? Pingback and trackback are an important part of WordPress to generate back links.

Anyways, I went out searching for a WordPress plugin to separate the pingback/trackback, but I couldn’t find any good plugins to separate from my comment area.

However, I found this page that showed you exactly what to do:

Managing Trackbacks and Pingbacks in Your WordPress Theme

Basically you add this line to your functions.php, inside your theme folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
add_filter('get_comments_number', 'filterCommentsNumber');
function filterCommentsNumber($count) {
	global $id;
	if (empty($id)) { return $count; }
	$comments = get_approved_comments((int)$id);
	$comments = array_filter($comments, "stripTrackback");
	return sizeof($comments);
}
add_filter('comments_array', 'filterComments', 0);
add_filter('the_posts', 'filterPostComments', 0);
//Updates the comment number for posts with trackbacks
function filterPostComments($posts) {
	foreach ($posts as $key => $p) {
		if ($p->comment_count <= 0) { return $posts; }
		$comments = get_approved_comments((int)$p->ID);
		$comments = array_filter($comments, "stripTrackback");
		$posts[$key]->comment_count = sizeof($comments);
	}
	return $posts;
}
//Updates the count for comments and trackbacks
function filterComments($comms) {
global $comments, $trackbacks;
	$comments = array_filter($comms,"stripTrackback");
	$trackbacks = array_filter($comms, "stripComment");
	return $comments;
}
//Strips out trackbacks/pingbacks
function stripTrackback($var) {
	if ($var->comment_type == 'trackback' || $var->comment_type == 'pingback') { return false; }
	return true;
}
//Strips out comments
function stripComment($var) {
	if ($var->comment_type != 'trackback' && $var->comment_type != 'pingback') { return false; }
	return true;
}

Also, inside the functions.php you might want to add this if you want to remove pingback/trackback from your Recent Comment on your widget.

1
2
3
4
5
6
/* START REMOVE TRACKBACK/PINGBACK FROM RECENT COMMENT WIDGET */
if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
    $comments = $wpdb->get_results("SELECT $wpdb->comments.* FROM $wpdb->comments JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID WHERE comment_approved = '1' AND post_status = 'publish' AND comment_type IN ('comment', '') ORDER BY comment_date_gmt DESC LIMIT 15");
    wp_cache_add( 'recent_comments', $comments, 'widget' );
}
/* END REMOVE TRACKBACK/PINGBACK FROM RECENT COMMENT WIDGET */

Then you open your comments.php inside your theme folder and insert the below code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php /* BEGIN TRACKBACK/PINGBACK CODE */ ?>
<?php global $trackbacks; ?>
<?php if ($trackbacks) : ?>
<?php $comments = $trackbacks; ?>
<div id="pingback-trackback">
<h3 id="trackbacks">There are <font color="#519007"><?php echo sizeof($trackbacks); ?> Trackbacks/Pingbacks</font> to "<?php the_title(); ?>"</h3>
	<ol class="commentlist">
 
	<?php foreach ($comments as $comment) : ?>
<!-- Start Your trackback Code -->
		<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">
			<cite><?php comment_author_link() ?></cite>
			<?php if ($comment->comment_approved == '0') : ?>
			<em>Your comment is awaiting moderation.</em>
			<?php endif; ?>
 
			<small class="commentmetadata"><?php edit_comment_link('edit','&nbsp;&nbsp;',''); ?></small>
 
			<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('edit','&nbsp;&nbsp;',''); ?></small>
 
			<?php comment_text() ?>
		</li>
<!-- End Your trackback Code -->
	<?php
		/* Changes every other comment to a different class */
		$oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : '';
	?>
 
	<?php endforeach; /* end for each comment */ ?>
 
	</ol>
</div>
<?php endif; ?>
<?php /* END TRACKBACK/PINGBACK CODE */ ?>

Be sure to check the original post at Managing Trackbacks and Pingbacks in Your WordPress Theme to get the full tutorial.

More WordPress Tutorials.


Related posts:

Why pingback/trackback not working on my WordPress web site?
How to submit sitemap to Google, Bing, Yahoo!, Ask
WordPress set up tutorial
How to add a new user with WordPress
CSS syntax tutorial

There are 1 Comments to "Separate Pingback/Trackback from Comments"

Write a Comment



(optional)
*