Categories: Web Development

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.

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

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.

Share
Published by
Tracy Ridge

Recent Posts

Hot New Web Dev – October 2024

Welcome to Hot Web Dev October 2024, featuring the latest technology and web development news.… Read More

3 weeks ago

New Svelte 5 Guessing Game 2024

In this tutorial, you’ll build a fun and interactive guessing game using Svelte 5, the… Read More

2 months ago

Hot New Web Dev – September 2024

Welcome to Hot Web Dev September 2024, featuring the latest technology and web development news.… Read More

2 months ago

New JavaScript Guessing Game 2024

The JavaScript guessing game tutorial is a simple beginner's project. It features modern JavaScript syntax… Read More

2 months ago

Hot Web Dev – 12 Useful Top Tools of 2023

If you have been following the monthly Hot Web Dev magazine you will find the… Read More

2 months ago

Hot New Web Dev – August 2024

Welcome to Hot Web Dev August 2024, featuring the latest technology and web development news.… Read More

3 months ago