post-page

Managing Trackbacks and Pingbacks in Your WordPress Theme

43
responses

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.

theme_setup.jpg
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:

functions_php.jpg
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.

comments_php.jpg
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.

trackbacks_comments.jpeg
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.

comments_wo_trackbacks.jpeg
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.

comment_sorter_admin.jpg
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.

heading
heading
43
Responses

 

Comments

  1. kosir (8 comments.) says:

    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.

  2. Angelfire (9 comments.) says:

    Excelent, so much thanks, but… Someone can translate this, my english is so bad, I need this post in spanish :(

  3. Darran (9 comments.) says:

    I have done the modifications as you said, worked like a charm! Thanks!

  4. Ronald Huereca (66 comments.) says:

    @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.

  5. simon (1 comments.) says:

    nice article, just wrote one on comments and Trackbacks and will update it to include your link…

  6. impuLsive (2 comments.) says:

    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.

  7. Ronald Huereca (66 comments.) says:

    @impuLsive
    Are you not receiving trackbacks at all, or just not showing up on your theme?

  8. Tee (1 comments.) says:

    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!

  9. impuLsive (2 comments.) says:

    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?

  10. Ronald Huereca (66 comments.) says:

    @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.

  11. Lloyd Lopez (3 comments.) says:

    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.

  12. Group Writing Projects (6 comments.) says:

    Great post, Ronald, and thanks for the code. I’ve added it to my to do list.

    Jacob

  13. Chess Teaching (15 comments.) says:

    Very informative post including some of the answers you have given in the comments section.

  14. Program Manager (1 comments.) says:

    I’m sure this will be handy!

  15. absolut-desing (1 comments.) says:

    thankyou for this creat tutorials
    have a nice day

  16. Ronald Huereca (66 comments.) says:

    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

  17. Darran (9 comments.) says:

    I know I already thanked you in the email, but here’s another one for a job well done.

  18. Ronald Huereca (66 comments.) says:

    You’re welcome Darran.

  19. Matt Algren (5 comments.) says:

    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.

  20. Brian H says:

    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

  21. kovshenin (13 comments.) says:

    Thank you, that was helpful.

  22. Nerabotai (1 comments.) says:

    Hey, may i steal some of the solutions?)

  23. Sebastien (1 comments.) says:

    Hey thanks. I used the second solution to filter pingback and trackbacks out. I always prefer editing the code than installing yet another plugin.

  24. Chris (2 comments.) says:

    Is this code still up to date?

  25. Chris (2 comments.) says:

    I’m having trouble figuring out where to drop the functions.php code. Any help?

  26. Aditya says:

    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.



Trackbacks/Pingbacks

  1. [...] Weblog Tools Collection has an excellent article, the topics they cover are on separating trackbacks/pingbacks from regular [...]

  2. [...] Weblog Tools Collection » Blog Archive » Managing Trackbacks and Pingbacks in Your WordPress Theme (tags: theme wordpress) [...]

  3. [...] Managing Trackbacks and Pingbacks in Your WordPress Theme (tags: wordpress) [...]

  4. RUDEWORKS says:

    [...] Se lo han currado mucho en la Weblog Tools Collection para separar los pingbacks de los comentarios, pero creo que me sigue convenciendo más la opción [...]

  5. [...] post explaining how to separate trackbacks and comments and expanded on it in their post explaining how to manage trackbacks and pingbacks in your WordPress theme.   While it isn’t quite as simple as our instructions, it will give you much more control [...]

  6. [...] Weblog Tools Collection » Blog Archive » Managing Trackbacks and Pingbacks in Your WordPress Theme Great article for Wp-developers (tags: howto theme themes tutorial webdesign wordpress comments trackbacks pingbacks) [...]

  7. [...] Managing Trackbacks and Pingbacks- This topic covers separating trackbacks/pingbacks from regular comments, and also how to remove [...]

  8. [...] Zum angesprochenen Artikel: Managing Trackbacks und Pingbacks in Your WordPress Theme [...]

  9. [...] to the theme templates and I want to remove that wherever possible. Then, a couple of weeks ago, this post on weblogtoolscollection talked about a similar topic and I noticed the comments_array plugin hook, a hook I hadn’t [...]

  10. [...] – Separate Trackbacks from Comments by using this nifty article as a [...]

  11. [...] I stumbled upon a 3 way approach to solve this problem at Weblog Tools Collection. Subscribe to comments on this post Trackback [...]

  12. [...] while back, I stumbled upon a post which tells of separating pings from comments by editing the function.php file. The method which I have used is to make use of filters to strip [...]

  13. [...] you are a technically inclined person and would like to do it to the theme yourself, you can read Managing Trackbacks and Pingbacks in Your WordPress Theme at Weblog Tools to learn how to get it [...]

  14. [...] and stripComments functions in the Trackback sorter code are from Ronald Huereca’s Managing Trackbacks and Pingbacks in Your WordPress Theme post on [...]

  15. [...] che di mio c’è ben poco: mi sono limitato a trasformare in plugin alcune funzioni di questo tutorial, per facilitare l’installazione a chi ha difficoltà a smanettare con codice [...]

  16. [...] indirip incelemek için Buraya T?klay?n?z. ?lgili konu hakk?ndaki ingilizce kaynak için http://weblogtoolscollection.com/archives/2008/03/08/managing-trackbacks-and-pingbacks-in-your-wordp… linkine [...]

Obviously Powered by WordPress. © 2003-2013

page counter
css.php