Skip to main content Jump to list of all articles

Display Your Twitter Posts On Your Web Site!

There are lots of resources out there for displaying your latest twitter posts in your website. Some of them require plugins or some may require javascript to work. After trying a few scripts I came across a little PHP script from The Lylo Files that displays up to 20 of your posts on your web site. After using it for a little while I decided to make it more functional.

This script has been depreciated in favor of the the new Twitter API. Check out Display your Cached Tweets Using PHP and OAuth Also checkout WOW-Multi-Twitter 2.0

The list of changes from the original are:

  • #Hashtags are now clickable.
  • Changed @reply to include _ underscores and – as many user-names contain these.
  • Added retrieval of published date and its associated link.
  • Split the date up so that you can have many different variants like Thu 10 Sep or Sep 10 2009, etc.

Setup

<?php
$doc = new DOMDocument();
 
# load the RSS -- replace 'worldoweb' with your username.
if($doc->load('https://twitter.com/statuses/user_timeline/worldoweb.rss')) {
  echo "<ul>n";
 
  # number of <li> elements to display.  20 is the maximum
  $max_tweets = 15;    
 
  $i = 1;

  foreach ($doc->getElementsByTagName('item') as $node) {
    # fetch the title from the RSS feed. 
	$tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
	
	#Fetch the link from the feed
	$link = $node->getElementsByTagName ('link') ->item(0)->nodeValue;
	
	# This gets the date and separates it into sections
	$pubDate = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
	$month = substr($pubDate, 7, 4);
	$date = substr($pubDate, 4, 3);
	$year = substr($pubDate, 11, 5);
	$day = substr($pubDate, 0 , 3);
 	$title=$day.$date.$month.$year;
	
	#pre-defined date lazy tags.  You can also make your own using $month,$date,$year & $day 
	$ddmmyy=$date.$month.$year;
	$mmyy=$month.$year;
	$mmddyy=$month.$date.$year;
	$ddmm=$date.$month;
	

    # the title of each tweet starts with "username: " which I want to remove
    $tweet = substr($tweet, stripos($tweet, ':') + 1);   
 
    # OPTIONAL: turn URLs into links
    $tweet = preg_replace('@(https?://([-w.]+)+(:d+)?(/([w/_./-]*(?S+)?)?)?)@', 
          '<a rel="nofollow" target="blank" title="$1" href="$1">$1</a>', $tweet);
		  
 	#OPTIONAL: turn hashtags into links
 	$tweet = preg_replace('/#([0-9a-zA-Z_-]+)/', 
	"<a rel="nofollow" target='blank' title='$1' href="https://twitter.com/search?q=%23$1">#$1</a>", $tweet);
 	
    #OPTIONAL: turn @replies into links
    $tweet = preg_replace("/@([0-9a-zA-Z_-]+)/", 
          "<a rel="nofollow" target='blank' title='$1' href="https://twitter.com/$1">@$1</a>", $tweet);
		 	  
 	#The following line can changed to suit your needs.
    echo "<li class='tweet'>"."<strong>"."<a rel="nofollow" href='$link' target='blank' title='$title'>". $ddmmyy ."</a>" ."</strong>"  .$tweet  ."</li>n";
  
	if($i++ >= $max_tweets) break;
  }
  echo "</ul>n";
} 
?>

It is pretty simple to setup just copy and paste the above code into your site, change worldoweb to your twitter username and change the $max_tweets to anything between 1 and 20. The default output, in line 51, displays the link at the beginning of the tweet wrapped in bold, strong tags.

Display twitter post on your site

To display the date at the end, change line 51 to:

echo "<li class='tweet'>". $tweet . "<strong>". "<a rel="nofollow" href='$link' target='blank' title='$title'>" . $ddmmyy."</a>" . "</strong>" . "</li>n";

Display twitter post on your site

You can of course remove the link altogether.

 echo "<li class='tweet'>" . "<strong>" . $ddmmyy . "</strong>" . $tweet . "</li>n";

The change the date format string in the tweet (line 51) you can change the default $ddmmyy to any of the following.

  • To display the date like 09 Sep change it to $ddmm
  • To display the date like Sep 09 change it to $mmyy
  • To display the date like Sep 09 2009 change it to $mmddyy
  • To add the day of the week to these use $day.$mmyy.

Conclusion

I hope you enjoyed this script and I would like to thank the original author of the script for making this possible. I would love to hear all of your comments so don’t be shy.

8 replies on “Display Your Twitter Posts On Your Web Site!”

Avatar of simarjeet singh
simarjeet singh
simarjeet singh On

how many time we are hit a url in day or month to twiter blog.?is there any limit?

so, please reply me as soon as possible
thanks

Comments are closed.