Display X most recent Twitter entries

Last modified: June 12, 2007 - 13:57

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;
}
?>

Linking to the tweet URL

victortrac - March 26, 2008 - 20:07

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;
}

Use Feedapi

Jody Lynn - April 5, 2008 - 22:02

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

should be...

hayesr - June 27, 2008 - 17:08

Linking to the tweet URL and turning text links into hrefs

tizzo - April 8, 2008 - 04:04

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;
}
?>

Linking to other Twitterers

tizzo - June 18, 2008 - 19:03

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.

<?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);
$tweet = ereg_replace("@[^<>[:space:]]+[[:alnum:]/]","<a href=\"http://www.twitter.com/\\0\">\\0</a>", $tweet);
$tweet = preg_replace('/@/', '${1}${3}', $tweet, 1);
  print
'<li><div class="my-twitter-status">'.$tweet.'</div><div class="tweet"><a href="'.$row->link.'">Original 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;
}
?>

@ sign remains in the link for twitter username

Bucketworks - March 19, 2009 - 17:53

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?

Bucketworks
The world's first health club for the brain
www.bucketworks.org
tw: bucketworks

Thank you!

jan.n - May 18, 2009 - 12:29

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 ;-)

Format changes

metarobert - October 14, 2009 - 22:45

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.

<?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.
 
$tweet = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $tweet);
$tweet = ereg_replace("@[^<>[:space:]]+[[:alnum:]/]","<a href=\"http://www.twitter.com/\\0\">\\0</a>", $tweet);
$tweet = preg_replace('/@/', '${1}${3}', $tweet, 1);
  print
'<p><div class="my-twitter-status">'.$tweet.'</div><div class="tweet"><a href="'.$row->link.'">Original Tweet</a></div>';
  print
'<div class="my-twitter-status-time" style="color:grey;">'.twitter_ago($row->timestamp).'</div></p>'."\n";
}
print
'</ul>';

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

This is great for us Drupal 5

Berto - May 15, 2009 - 04:48

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;
}
?>

Any ideas as to why when I

Qwert302 - August 26, 2009 - 06:03

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.

twitter module

alexkb - June 8, 2009 - 14:06

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.

Blog: akb.id.au | Twitter: @alexbergin | Company: drupalise

The twitter module looks

lkd - July 1, 2009 - 15:10

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?

Drupal 6?

Summit - December 4, 2009 - 13:18

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

Greetings,
Martijn www.trekking-world.com

 
 

Drupal is a registered trademark of Dries Buytaert.