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.
Great post. I’ll try your plugin and maybe I’ll even use it. I’m not sure yet and actually I like to see trackbacks and pingbacks in my blog. But it could be a problem with more popular blogs that spammer hit more then mine. Will keep this in mind if it ever comes to this.
Excelent, so much thanks, but… Someone can translate this, my english is so bad, I need this post in spanish 🙁
I have done the modifications as you said, worked like a charm! Thanks!
@kosir,
That’s great. Just one thing to keep in mind: none of the techniques actually stop trackbacks from coming in. They just prevent them from showing.
@Angelfire,
I would but my Spanish is so horrible also 🙂 Can anyone help out? I’ll provide a link to the translated post if so.
nice article, just wrote one on comments and Trackbacks and will update it to include your link…
I would love If you told me how to get trackbacks working with my installation of WP 2.3. I researched, came up with nothing. It didn’t work with and without all plugins activated and deactivated, even with a fresh install.
@impuLsive
Are you not receiving trackbacks at all, or just not showing up on your theme?
Wow, really a lot of great information here, I had no idea that this amount of customization was so readily available.
Angelfire – did you try running this URL through babelfish.altavista.com and choosing English to Spanish? The translations can be a little rough, but you should get the idea. Hope that helps!
When I send trackbacks to other blogs, they don’t appear. It appeared on one blog with a delay. Do some have em on moderation, or does WP have some kind of delay?
@impuLsive,
Some have them in moderation, some are automatically sent to Akismet Spam. And some blogs don’t even have them enabled. It’s really unpredictable and I don’t think you’re doing anything wrong if you’re using WordPress since WordPress automatically pings the other blogs.
I’ll try this on my blog. I really want to sort out the trackbacks and pingbacks on my blog for a long time now. This will help me a lot.
Great post, Ronald, and thanks for the code. I’ve added it to my to do list.
Jacob
Very informative post including some of the answers you have given in the comments section.
I’m sure this will be handy!
thankyou for this creat tutorials
have a nice day
Hey everyone, I’ve updated this post to be compatible with WordPress 2.5. See the second to last heading: Update for WordPress 2.5
I know I already thanked you in the email, but here’s another one for a job well done.
You’re welcome Darran.
Thanks for the update. I love having my pingbacks split out. Well, theoretically. I don’t get too many yet. [sad face]
I was coming back to this post to see if anybody else had a problem with their main comments feed after doing this modification. My comments feed (which is run through feedburner) has stopped updating, and it seems like it broke around the time I split the pingbacks out. Could this cause that problem?
I’m just troubleshooting and I want to rule this one out. Thanks.
Hi,
Great post. We’re running into a similar issue with WordPress Mu. Pingbacks from outside our domain are showing up fine; however pingbacks within our domain (i.e., our bloggers commenting on each other’s blogs) are not happening. Any thoughts?
Thanks,
BH
Thank you, that was helpful.
Hey, may i steal some of the solutions?)
Hey thanks. I used the second solution to filter pingback and trackbacks out. I always prefer editing the code than installing yet another plugin.
Is this code still up to date?
I’m having trouble figuring out where to drop the functions.php code. Any help?
Everything’s fine and good. I get all the counts right but the trackbacks and the pingbacks aren’t shown. I get the number right… say 9 trackbacks/pingbacks to this post but I don’t see the links. Please help? I’ve enabled the trackbacks option from the Discussion settings as well.