RSS Feed
  • Would you buy a 3D TV?

    View Results

    Loading ... Loading ...
wordpress-filtercomments

Filtering Comments in WordPress: Removing trackbacks and pingbacks from posts

Date: July 23rd, 2010

Author: Joe DiFiglia

Category: Wordpress

Tags: ,

Article URL: http://computingondemand.com/?p=4108


Rate this Post:

0 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 50 votes, average: 0.00 out of 5 (0 votes, average: 0.00 out of 5)
You need to be a registered member to rate this post.
Loading ... Loading ...

Facebook:

  • 1
  • Comments
  • EmailPrint This PostFeedShareCOD

As WordPress becomes more popular in the Web Designer circles (is that possible?), many of the functions we are looking for are simply not integrated by default.  One of the functions I searched for for a long time was a way to filter comments so pingbacks and trackbacks wouldn’t be displayed on my posts or pages.  I personally don’t care much for a list of websites linking to my post or article being shown and inundating the discussion with useless information.

My personal preference is to do this through my functions.php file instead of using the dashboard to disable them, I still want them… I just don’t want them to show.

Trackback helps you to notify another author that you wrote something related to what he had written on his blog, even if you don’t have an explicit link to his article. This improves the chances of the other author sitting up and noticing that you gave him credit for something, or that you improved upon something he wrote, or something similar. With pingback and trackback, blogs are interconnected. Think of them as the equivalents of acknowledgements and references at the end of an academic paper, or a chapter in a textbook.

Pingback lets you notify the author of an article if you link to his article (article on a blog, of course). If the links you include in an article you write on a blog lead to a blog which is pingback-enabled, then the author of that blog gets a notification in the form of a pingback that you linked to his article.

The following code will filter trackbacks and pingbacks from your comment list and adjust the number of comments accordingly.

1. Edit your theme’s functions.php file
2. Paste the following code between the <?php and the ?>

// --------------------------------------------------------------------------
// Start Comment Filter
// --------------------------------------------------------------------------
	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;
	}
	if ( function_exists( 'add_theme_support' ) )
// --------------------------------------------------------------------------
// End Comment Filter
// --------------------------------------------------------------------------

Enjoy your filtered comments!

Note: These functions are not of my own creation, simply things I have found along the way.  If you are the author of this function, please let me know so I can properly credit you with your work!

RSS Feed Share to Facebook Share to Twitter Stumble It Digg Linked In Technorati Delicious

Joe DiFiglia has written 136 Articles at C.O.D.


In early 2000 I became increasingly frustrated with hardware review sites praising less then satisfactory products. The saying: “if you want something done right, do it yourself” applies here. I wasn’t satisfied, so I did it myself; and here we are years later.