The Smashing Editorial

About The Author

The Smashing Editorial The Smashing team loves high-quality content and cares about the little details. Through our online articles, Smashing Books, eBooks as well as Smashing Conferences, we are committed to stimulating creativity and strengthening the web design community’s creative forces.

Introducing Tweetbacks Plugin for Wordpress

Just yesterday we introduced Twitter Avatars In Comments plugin for Wordpress that enables bloggers to have both Twitter and Gravatar avatars in blog comments. We are certainly not going to turn Smashing Magazine into a repository of Twitter plugins for Wordpress, but we are confident that it does make sense to introduce another plugin that enables another kind of Twitter integration in blogs....

Just yesterday we introduced Twitter Avatars In Comments plugin for Wordpress that enables bloggers to have both Twitter and Gravatar avatars in blog comments. We are certainly not going to turn Smashing Magazine into a repository of Twitter plugins for Wordpress, but we are confident that it does make sense to introduce another plugin that enables another kind of Twitter integration in blogs.

Tweetbacks

This is why this post releases the Twittbacks WordPress Plugin that was developed and released especially for Smashing Magazine — this plugin imports tweets about your posts as comments. You can display them in between the other comments on your blog, or display them separately.

Advantages of our implementation

This implementation is inspired by Dan Zarrella’s great work on TweetBacks. Although his implementation does what it promises, we felt that it could be done in another way and offer users an alternative to display tweets in a more convenient way.

Dan’s solution uses JavaScript, which will work on more platforms, but we felt that it would be useful to actually store the tweets in the database instead of using JavaScript to display them. Because our implementation stores Tweets in the database, you can create new plugins that use this data on the top of our plugin.

Besides, our implemenation does not does not require JavaScript and requires significantly less time to load (server-side, not client-side). (Because we know you want to test this, and be able to ditch the plugin if you don’t like it, we’ve added an uninstall function, which will delete all the stored tweets from your WordPress database.)

Installation

The installation couldn’t be simpler:

  1. download the plugin from Joost de Valk’s release post,
  2. copy the “tweetbacks” folder to your plugin folder (wp_root/wp-content/plugins);
  3. login inside your WordPress administrator panel and activate this plugin (Click “Plugins” and then “Activate” near to the plugin title);
  4. if you are using Wordpress 2.7, just add the code presented in the next section of this post (Styling and Adding Tweetbacks in Wordpress 2.7) and you are done.
  5. otherwise if you are using older versions of Wordpress, you can add Twitterbacks similarly to the way it is presented in this tutorial:

    1. Access your comments.php file and locate the following code:

      <?php foreach ($comments as $comment) : ?>

      Immediately after the above code, place this code:

      <?php
        $comment_type = get_comment_type();
        if($comment_type == ‘comment’) {
      ?>

    2. Next, scroll down a little bit and locate the following code:
      <?php endforeach; /* end for each comment / ?&gt

      Put the following code right before the above code:

      <?php } / End of is_comment statement */ ?>

      This will filter out all of the tweetbacks, trackbacks and pingbacks from your main comments loop. Now we need to create a second comments loop to display tweetbacks, and a third loop to display the trackbacks and pingbacks.

    3. Right under the code from step 2 you should find this code:

      <?php else : // this is displayed if there are no comments so far ?>

      Put the following code right before the above code:

      <h3>Tweetbacks</h3>
      <ol>

      <?php foreach ($comments as $comment) : ?> <?php $comment_type = get_comment_type(); ?> <?php if($comment_type == ‘tweetback’) { ?> <li><?php comment_author_link() ?>: <?php comment_text() ?></li> <?php } ?> <?php endforeach; ?>

      Followed by this:

      <h3>Trackbacks</h3>
      <ol>
      <?php foreach ($comments as $comment) : ?>
        <?php $comment_type = get_comment_type(); ?>
        <?php if($comment_type != 'comment' && $comment_type != 'tweetback') { ?>
          <li><?php comment_author_link() ?></li>
        <?php } ?>
      <?php endforeach; ?>
      </ol>
    4. And you are done.

Styling and Adding Tweetbacks in Wordpress 2.7

As the plugin will import these tweets into your comments, they will by default be shown as normal comments on your blog. They will get the class tweetback. If you're using WordPress 2.7 and the default theme, or any other theme that has taken the styling from the comments from the default theme (which is a sensible thing to do), you could style these tweetbacks by adding something like this to your stylesheet:

.commentlist li.tweetback {
  background-color: #94E4E8;
}

If you want to display them separately, you'll have to separate your comments, pingbacks and tweetbacks from each other. A great tutorial on how to do that is Separating Pings from Comments in WordPress 2.7.

After you've done that, you can add the following code to display the tweetbacks:

if ( !empty($comments_by_type['tweetback']) ) :?>
  <h2 id="tweetbacks"><?php echo count($comments_by_type['tweetback']);?> Tweetbacks to &#8220;<?php the_title(); ?>&#8221;</h2>
  <ul class="commentslist tweets">
    <?php wp_list_comments('type=tweetback'); ?>
  </ul>
<?php
endif;

If you want to give that list of tweetbacks a different look and feel, you can add a callback to the wp_list_comments call above. The callback could make them look like this, for instance:

Separated Tweetbacks

The code for the callback used above looks like this:

function yoast_list_tweetbacks($comment, $args, $depth) {
  $GLOBALS['comment'] = $comment;
  $avatarurl = str_replace("twitter:","http://s3.amazonaws.com/twitter_production/profile_images/",$comment->comment_author_email);
?>
  <li class="tweetback" id="comment-<?php comment_ID(); ?>">
    <div id="div-comment-<?php comment_ID(); ?>">
      <div class="comment-author vcard">
        <img alt='' src='<?php echo $avatarurl ?>' class='avatar avatar-40 photo avatar-default' height='40' width='40' />
        <cite class="fn"><a href="<?php echo $comment->comment_author_url; ?>" rel='external nofollow' class='url'><?php comment_author(); ?></a></cite> <span class="says">says:</span>
      </div>
      <div class="comment-meta commentmetadata"><a href="#comment-<?php comment_ID(); ?>"><?php comment_date('F jS, Y'); ?> at <?php comment_date('H:i:s'); ?></a></div>
      <p><?php comment_text(); ?></p>
    </div>
  </li>
<?php }

Because these tweetbacks are stored as comments in your database, you can all sorts of cool things with them. Improving the Web has written a Widget pack that will work with this plugin, called Tweet Stats. It allows you to add widgets with "Recently tweeted posts" and "Most tweeted posts" (see examples below).

Displaying Twitter in a variety of ways

If you have any installation problems, questions or suggestions for this plugin, please either comment on this post or add your suggestions in the support forum on Wordpress.org.

About the author

The plugin was developed by Joost de Valk, a Dutch geek and WordPress developer. You can follow him on Twitter.


More Articles on

Twitter Avatars In Comments Wordpress Plugin

by The Smashing Editorial

Over the last months Twitter has become extremely popular across the Web, with more blogs, magazines and companies using it to communicate with their audience and their customers. Even we started twittering a couple of months ago and have over 3,650 followers now. Please feel free to join us and follow Smashing Magazine on Twitter as well. Although we aren't sure if "Twitter" will...

Read more

10 Killer WordPress Hacks

by Jean-Baptiste Jung

2008 was a very good year for the WordPress community. The software was updated numerous times, leading to the recent release of version 2.7, and many new blogs dedicated to WordPress were created. Of course, tons of new hacks were discovered, which helped lots of bloggers enhance their blogs. In this article, we'll show you 10 new useful killer WordPress hacks to unleash the power of...

Read more

Free Icons, Photoshop Brushes and a WordPress Theme

by The Smashing Editorial

We are definitely not Santa Claus, but we love to release high-quality sets and themes for free. And, of course, we do have some beautiful and unusual and (hopefully) useful presents for the upcoming New Year's Eve. Today we are glad to release three icon sets, one Photoshop brushes set and one exclusive WordPress-theme. A couple of more presents will be released over the next days. ...

Read more