Last updated 12 June 2007. Created on 12 June 2007.
Edited by wayfarer_boy. Log in to edit this page.

Just thought I'd post a little block code for displaying your X most recent Twitter entries. You need to add your Twitter rss feed to the aggregator, and then modify the code below with your feed ID.

<?php
$fid = 4; // Add your Twitter rss feed to the aggregator, then put the feed id here
$limit = 5; // Change this to the number of entries you'd like to show
$result = db_query('SELECT description, timestamp FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,'.$limit, $fid);
print '<ul>';
while($row = db_fetch_object($result)) {
	print '<li><div class="my-twitter-status">'.substr($row->description, 4).'</div>'; // I removed the first 4 characters from the  description, as I didn't want each line to read 'Al: did this'... is there a setting I can change in my Twitter account for this prefix to be removed?
	print '<div class="my-twitter-status-time">'.twitter_ago($row->timestamp).'</div></li>';
}
print '</ul>';

// Thanks to drenei for posting this function - drupal.org/node/61565
function twitter_ago($timestamp){
   $difference = time() - $timestamp;
   $text = format_interval($difference, 2) . " ago";
   return $text;
}
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

victortrac’s picture

Thanks for this.. saved me a few minutes. I decided to add the link to the tweet as well:

<?php
$fid = 5;
$limit = 5;

$result = db_query('SELECT description, timestamp, link FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,' . $limit, $fid);

print '<ul>';
while($row = db_fetch_object($result)) {
  print '<li><div class="my-twitter-status"><a href="'.$row->link.'">'.substr($row->description, 12).'</a></div>';
  print '<div class="my-twitter-status-time">'.twitter_ago($row->timestamp).'</div></li>'."\n";
}
print '</ul>';

function twitter_ago($timestamp){
   $difference = time() - $timestamp;
   $text = format_interval($difference, 2) . " ago";
   return $text;
}
Jody Lynn’s picture

You can also use feedapi module: http://drupal.org/feedapi . You will also need views module for this approach.

Install feedapi module and enable it and its submodules (simplepie parser and feedapi node). Follow the instructions for simplepie parser installation which requires downloading the simplepie class.

Create a new content type called e.g. 'Tweet'.
Add a new 'feed' node (feedapi adds a new content type called 'feed'). Include the link to your twitter rss feed in this node. In the 'processers' fieldset assign the incoming feed items to the content type 'Tweet.'

Now each of your tweets will be saved as a node of type tweet.
Create a new block view which filters on Node type='tweet'. In the view block footer you can add a link to "Follow me".

--Zivtech--

hayesr’s picture

tizzo’s picture

I find myself posting links often and didn't want to make my readers paste the link into their address bars. This update adds a regexp that parses the tweet and turns any text starting with http into a link. I moved the link to the original tweet into a link below the update but above the display of the time since the update.

<?php
$fid
= 1; // Add your Twitter rss feed to the aggregator, then put the feed id here
$limit = 5; // Change this to the number of entries you'd like to show
$result = db_query('SELECT description, timestamp, link FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,' . $limit, $fid);
print
'<ul>';
while(
$row = db_fetch_object($result)) {
 
$tweet = substr($row->description, 6);  // Change this to the number of characters to remove from the beginning of each post.  This allows you to remove username: from the beginning of each post.
 
$tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href="\\0">\\0</a>", $tweet);
  print
'<li><div class="my-twitter-status">'.$tweet.'</div><div class="tweet"><a href="'.$row->link.'">Tweet</a></div>';
  print
'<div class="my-twitter-status-time">'.twitter_ago($row->timestamp).'</div></li>'."\n";
}
print
'</ul>';
function
twitter_ago($timestamp){
  
$difference = time() - $timestamp;
  
$text = format_interval($difference, 2) . " ago";
   return
$text;
}
?>
tizzo’s picture

I also wanted to be able to provide links to the people that I messaged in my tweets (making any instance of @SomeUserName link back to http://www.twitter.com/SomeUserName). After a little more regexp magic this snippet does that.

$fid = 1; // Add your Twitter rss feed to the aggregator, then put the feed id here
$limit = 5; // Change this to the number of entries you'd like to show

$result = db_query('SELECT description, timestamp, link FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,' . $limit, $fid);

print '
    '; while($row = db_fetch_object($result)) { $tweet = substr($row->description, 6); // Change this to the number of characters to remove from the beginning of each post. This allows you to remove username: from the beginning of each post. $tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","\\0", $tweet); $tweet = ereg_replace("@[^<>[:space:]]+[[:alnum:]/]","\\0", $tweet); $tweet = preg_replace('/@/', '${1}${3}', $tweet, 1); print '
  • '.$tweet.'
    '; print '
    '.twitter_ago($row->timestamp).'
  • '."\n"; } print '
'; function twitter_ago($timestamp){ $difference = time() - $timestamp; $text = format_interval($difference, 2) . " ago"; return $text; }
Bucketworks’s picture

I installed this wonderful code in a block, and the @ sign remains in the URL generated by the regexp for the twitter username referenced in a post. Any idea how to fix this?

jan.n’s picture

I use this code on my front page to display my latest tweets!
I really don't understand though why, until now, nobody has written a reverse twitter integration...

jan

PS: No, I don't have the time to write it myself - I have a daughter and my job is quite challenging ;-)

metarobert’s picture

I really liked this code, but wanted to be a bit more frugal with use of the small space in the sidebar. I changed this from a list of tweets to use paragraphs so as not to lose the indented space from the list. I also set the timestamp to show as grey text. I like it this way so it is less prominent, and actually helps create a visual cue as to where a tweet ends.

Sorry if I've goofed anywhere here. I'm brand new to Drupal and this forum.

$fid = 1; // Add your Twitter rss feed to the aggregator, then put the feed id here
$limit = 5; // Change this to the number of entries you'd like to show

$result = db_query('SELECT description, timestamp, link FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,' . $limit, $fid);

print '
    '; while($row = db_fetch_object($result)) { $tweet = substr($row->description, 11); // Change this to the number of characters to remove from the beginning of each post. This allows you to remove username: from the beginning of each post. $tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","\\0", $tweet); $tweet = ereg_replace("@[^<>[:space:]]+[[:alnum:]/]","\\0", $tweet); $tweet = preg_replace('/@/', '${1}${3}', $tweet, 1); print '

    '.$tweet.'
    '; print '
    '.twitter_ago($row->timestamp).'
    '."\n"; } print '
'; function twitter_ago($timestamp){ $difference = time() - $timestamp; $text = format_interval($difference, 2) . " ago"; return $text; }
Berto’s picture

This is great for us Drupal 5 users, thanks guys!

I don't want my replies to get onto my website ( http://www.priceplow.com ), so I am ignoring anything that begins with @. I also made some changes to the tweet HTML:

<?php
$fid = 1; // Add your Twitter rss feed to the aggregator, then put the feed id here
$limit = 5; // Change this to the number of entries you'd like to show

$result = db_query('SELECT description, timestamp, link FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC LIMIT 0,' . $limit, $fid);

print '<ul>';
while($row = db_fetch_object($result)) {
  
  $tweet = substr($row->description, 11);  // Change this to the number of characters to remove from the beginning of each post.  This allows you to remove username: from the beginning of each post.

  if(strpos($tweet, '@') === FALSE) {
    $tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $tweet);
    print '<li><div class="my-twitter-status">'.$tweet.'</div><div class="tweet"><a href="'.$row->link.'" target="_blank">Tweet</a> <small>('.twitter_ago($row->timestamp).')</small></div></li>'."\n";
  }
}
print '</ul>';

function twitter_ago($timestamp){
   $difference = time() - $timestamp;
   $text = format_interval($difference, 2) . " ago";
   return $text;
}
?>
Ross-Hunter’s picture

Any ideas as to why when I try to use this snippet twice for 2 separate blocks (same code, just different feed id) it crashes the whole site? I want to display 2 separate boxes with tweets from 2 separate feeds but I can't. Both blocks work when enabled individually. It's perplexing to me.

alexkb’s picture

Is there anything wrong with the twitter module for achieving this sort of thing?

If you want to use drupal to blog and only show your own tweets (as it defaults to use the users id when viewing their profile), just configure the main twitter settings with your own twitter account, and then edit the twitter view not to require an argument. You can also set up the twitter account settings to tweet a quick message (with an automated tinyurl linl) when you post a blog post. It's brilliant!

I'm still setting some other things up on my blog at present, so its not live, but if anyways interested in an example, pm me and I'll send a staging site link. Cheers.

lkd’s picture

The twitter module looks awesome, but complicated (which is a euphemism for "I can't figure it out").

I want to select one twitter account, which I want displayed on various pages. I'm in the edit mode for the twitter stuff ("Edit view tweets") and it's not obvious to me what I should do. I tried to set UID to be fixed to the twitter account name, but that doesn't seem to work.

What's the right approach here?

Summit’s picture

Hi, How about a Drupal 6 snippet please?
Thanks a lot in advance for considering! Greetings, Martijn