Managing Trackbacks and Pingbacks in Your WordPress Theme 35comments
Thanks for visiting! We would like to serve you better. Please subscribe to our RSS feed for daily updates. This blog posts regular Wordpress news, updates of themes, plugins, ideas, hacks, quick fixes and everything about blogging, especially about Wordpress. You can also receive updates from this blog via email if you want that method of notification.
With all of the recent discussion regarding trackbacks and pingbacks on Weblog Tools Collection, I thought I’d mention several ways one can deal with trackbacks and pingbacks in the context of a WordPress theme.
The topics I will be covering in this article are on separating trackbacks/pingbacks from regular comments, and also how to remove trackbacks and pingbacks from a WordPress theme completely.
Separating Trackbacks/Pingbacks From Comments
I know what you’re thinking: numerous posts have already been written on how to separate trackbacks from comments.
But what I present here is an actual separation using the “functions.php” feature for WordPress themes along with the regular “comments.php“. Both should be located in your theme directory.

Figure 1: Theme Directory Setup
Modifying the functions.php File
The “functions.php” file is a lifesaver for any theme developer or tinkerer wishing to add custom code or functions to themes. The code in the “functions.php” file can be called from all theme files, and can act as makeshift WordPress plugins (without the need for activation).
What we’ll be doing here is adding four functions and two filters into the file.
You don’t really have to understand the functions, but here are some brief explanations:

Figure 2: functions.php Filter and Function Additions
- filterPostComments: Updates the comments number for all posts so that trackbacks aren’t included in the count.
- filterComments: Separates the trackbacks from the comments as global variables.
- stripTrackback: Strips trackbacks from an array.
- stripComment: Strips comments from an array.
The two filters we add in are explained below:
- comments_array: Basically the comments before they are read in the comments loop.
- the_posts: An array of all found posts.
Ok, enough with the explanations. Here’s the code for the “functions.php” file:
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;
}
Do you need to know how it works? Not really. But in summary, it separates comments from trackbacks and updates the comment count for all posts.
Modifying the comments.php file
Unlike most separation tutorials, we will not be modifying the comments loop.
In essence, we are expanding on a previous tutorial that also uses the “functions.php” file for separation of trackbacks and comments.
What we’ll be doing is the following:
- Adding a “trackbacks” global variable right after the comments loop.
- Initiating a trackbacks/pingbacks loop.

Figure 3: comments.php Load Order
Right after the comments loop ends, you would add the following:
<?php global $trackbacks; ?> <?php if ($trackbacks) : ?> <?php $comments = $trackbacks; ?> <h3 id="trackbacks"><?php echo sizeof($trackbacks); ?> Trackbacks/Pingbacks</h3> <ol class="commentlist"> <?php foreach ($comments as $comment) : ?> <!-- Your trackback HTML --> <?php endforeach; /* end for each comment */ ?> </ol> <?php endif; ?>
The code looks just like the comments loop except for a few lines of code, and the best part is, no editing of the original comments loop was necessary.
Download the Code
Here is a downloadable copy of the code presented in this post. The files within this zip file are:
- Sample “functions.php” file.
- Sample “comments.php” file.
I realize this solution is not the simplest demonstration of comment/trackback separation, but it allows for two actual and separate loops, and also produces a valid comments number(comments with trackbacks/pingbacks subtracted out) for all posts.
Removing Trackbacks/Pingbacks from WordPress Themes - Three Ways
When I asked the readers here if trackbacks were still useful, several expressed that they would be willing to remove trackbacks/pingbacks from comments.
With WordPress, I have found three ways to remove trackbacks and pingbacks from a WordPress theme.
Method 1: Edit the comments.php File
Located in almost every theme directory is a file called “comments.php“. Within this file is what’s known as the Comments Loop, which is what displays all of the comments for a post.
The start of Comments Loop looks like this:
<?php foreach ($comments as $comment) : ?>
To remove trackbacks/pingbacks from your theme, simply insert this code in the line right after the start of the loop:
<?php if ($comment->comment_type == "pingback" || $comment->comment_type == "trackback") { continue; } ?>
The above code skips over the trackbacks and pingbacks. The disadvantage of this method is that WordPress will still display the number of comments with trackbacks and pingbacks in the count.
![]()
Figure 4: Comments count with trackbacks included in the count
Method 2: Edit the functions.php File
Most themes should already come with a file named “functions.php“. If not, you can easily create one using any text editor.
Any code or functions in your “functions.php” file is immediately accessible by all of your theme files. The benefit of removing trackbacks/pingbacks using this technique is that you won’t have to modify any of the core template files and risk messing up your theme.
Within the functions.php file, insert this code:
add_filter('comments_array', 'filterTrackbacks', 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 filterTrackbacks($comms) { global $comments, $trackbacks; $comments = array_filter($comms,"stripTrackback"); return $comments; } //Strips out trackbacks/pingbacks function stripTrackback($var) { if ($var->comment_type == 'trackback' || $var->comment_type == 'pingback') { return false; } return true; }
This code is very similar to the code I used for separating trackbacks from comments.
Although not nearly as simple as the “comments.php” method, this method is more flexible and provides WordPress with an actual number of comments without the trackbacks/pingbacks being counted.
![]()
Figure 5: Comments count without trackbacks included
Method 3: The Plugin Solution
For those not wishing to modify themes, there is always the plugin solution.
A plugin I wrote called Comment Sorter has a feature that allows a blogger to remove trackbacks/pingbacks (not permanently) from within the WordPress Administration Panel. There is absolutely no theme modification involved.

Figure 6: Comment Sorter Admin Interface
Using the above configuration for Comment Sorter (with the auto-include off and trackbacks disabled), one can easily remove trackbacks and pingbacks from a theme. It’s basically Method 2 in the form of a plugin.
Update for WordPress 2.5 - May 1, 2008
A reader pointed out that the comment count is incorrect on WordPress 2.5. For both techniques, you will want to exclude the filter the_posts and the function filterPostComments. Instead, use this filter and function:
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); }
Conclusion
Within this post I presented techniques on separating trackbacks/pingbacks from regular comments, and also how to remove trackbacks and pingbacks from a WordPress theme.
If you have any questions, please leave a comment and I’ll do my best to address them.





































