Handling WordPress Trackbacks with jQuery

Posted on April 14, 2008 in Tutorial | 5 Comments »

So after having a ton of trackbacks in a recent article, The 6 Most Important CSS Techniques You Need To Know, I thought that it was kind of breaking up the flow of the comments.

I didn’t necessarily want to completely remove them because I thought that some people might be interested in them. These are the steps that I want to take:

  1. Assign a class to all trackbacks
  2. Hide them all with CSS
  3. Add a jQuery visibility toggle

1) Assign a Class to All Trackbacks

In your comments.php theme file, find a line that looks like this:

<li id="comment-<?php comment_ID() ?>">

And replace it with this:

<li class="<?php if(get_comment_type() != 'comment') echo 'trackback'; ?>" id="comment-<?php comment_ID() ?>">

Explanation

The get_comment_type function returns either comment, trackback, or pingback. Since I want to hide everything except comments, I filter out anything that does not equal comment, so I assign a class of trackback to that list item.

2) Hide them All with CSS

Ok, easy enough, just add the following to your stylesheet:

ol.commentlist li.trackback { display: none; }

That should be pretty self explanatory.

3) Add a jQuery Visibility Toggle

The logic of the JavaScript will be as follows:

  1. Check to see if there are any list items with a class of trackback.
  2. Add a link to Show/Hide trackbacks.
  3. Add a click event to the Show/Hide trackback link.
  4. Update the text of the link accordingly.

First, let’s execute the JavaScript once the document is ready:

$(document).ready(function(){

});

Next, let’s check to see if there are any elements with a class of trackback:

$(document).ready(function(){
	if($('.trackback').length > 0) {

	}
});

Next, if there are trackbacks on the page, insert a link to show trackbacks:

$(document).ready(function(){
	if($('.trackback').length > 0) {
		$('#comments').after('<a href="#" id="toggleTrackbacks">(Show Trackbacks)</a>');
	}
});

I am adding the show/hide trackback link after the heading with an id of comments.

Finally, I want to add in the click event to the link, and toggle the text depending upon if the trackbacks are visible or not:

$(document).ready(function(){
	if($('.trackback').length > 0) {
		$('#comments').after('<a href="#" id="toggleTrackbacks">(Show Trackbacks)</a>');
		$('#toggleTrackbacks').click(function() {
			$('.trackback').slideToggle('slow');
			if($('#toggleTrackbacks').text() == '(Show Trackbacks)') {
				$('#toggleTrackbacks').text('(Hide Trackbacks)');
			} else {
				$('#toggleTrackbacks').text('(Show Trackbacks)');
			}
			return false;
		});
	}
});

So basically, I am adding the jQuery slideToggle effect when the show/hide trackbacks link is clicked. Then, if the text in the show/hide trackbacks link is Show Trackbacks, I change it to Hide Trackbacks, and the opposite when the text is Hide Trackbacks.

I have grabbed some of the comments from The Ultimate PNG Guide so you can see a demo of this in action.

I love how simple jQuery is.

Share This:
  • NewsVine
  • Technorati
  • Reddit
  • Google
  • StumbleUpon
  • Facebook
  • Digg
  • del.icio.us
  • Ma.gnolia
  • TwitThis

5 Responses

  1. Mike SchinkelApril 15, 2008 at 2:53 pm

    Brilliant! I’ve always disliked how distracting trackbacks are in the comment stream. This is exactly the type of thing I was talking about when I made those comments to you offline about your theme. Bravo!

  2. TrevorApril 15, 2008 at 3:04 pm

    @Mike-
    Great, glad it’s useful. Hopefully in the next few months I will have my theme available for download.

    BTW, thanks for letting me know about the tab order of my comment form. I hate the tabindex attribute; it does nothing but cause trouble.

  3. CalaelenMay 12, 2008 at 9:15 am

    Why do you want to hide trackbacks?

    I mean, they are “comments” aswell. Well, comments that were written on another site instead of a direct answer under your blog entry.

    That’s what blogging was all about in the past? :o

  4. TrevorMay 12, 2008 at 5:12 pm

    @Calaelen-
    I don’t want trackbacks to display by default because they break up the flow of the comments. Just like you said, trackbacks are “comments”.

    If people are reading the comments on one of my blog entries, they typically are not coming to see what sites have linked to it. This is why I still want trackbacks available if people want to see them.

  5. StumbleUponAugust 16, 2008 at 4:28 pm

    Thanks for the informative post.. and thanks for adding our comment to the blog. I am subscribing to your feed so I don't miss the next post!

Speak Your Mind

* Denotes Required Field

  1. Sick of filling out this form? Register or Log in now.

Who Am I?

Trevor Davis I’m Trevor Davis, a 24 year old Front-End Developer. Basically, I make web sites.

By day, I work for Matrix Group International in Alexandria, VA, and by night, I freelance.

Feel free to get in touch with me about anything.

What Have I Done?

  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In

View All My Work »

Bookmarks

  • Google Search Engine Optimization Starter Guide [PDF]

    Google has released a free 22-page Search Engine Optimization Starter Guide containing plenty of well-written, practical and straightforward advice for webmasters. If you've been looking into SEO for a while it probably won't contain anything new for you, but it's useful as a set of guidelines as to what Google considers to be good optimization practice. (psst, Google, with just a little design work it could have looked so much nicer!)

  • The importance of setting expectations

    To make your customer's experience better, be sure to set their expectations.

  • XML Sitemaps Generator

    Insert your URL and let it generate the XML sitemap for you. Very useful for static websites.

  • Train-ee ExpressionEngine Training

    Learn ExpressionEngine with books, screencasts, classroom training and free tutorials from Train-ee.com

  • web.without.words

    Weekly gallery of popular websites reconstructed by removing all words and images, replacing them with blocks.

View All My Bookmarks »