How To Add A Twitter Feed To Your Website

It is very simple to add a Twitter feed to your own website without using Twitter’s default UI widget, which frankly looks quite ugly. If you want full customization ability, keep on reading and I’ll show you what to do using a bit of PHP.

To pull the Tweets from the Twitter API I have created a PHP Function with a few parameters in order to give it some extra flexibility. You are for example able to change the username, amount of tweets to show and whether to show or hide the username that appears first in the tweet when it is pulled from the API.

The Code

Here is the function in question. If you are using WordPress you should consider turning this into its own plugin so that it is available regardless of the theme, however if you really just need it quickly, drop this into your functions.php file.

function latest_tweets($username, $amount = 2, $removeUserID = true){

    $doc = new DOMDocument();
    $feed = "http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=$username";

    $doc->load($feed); 

      $outer = '<ul id="tweets">';

      $i = 1;

      foreach ($doc->getElementsByTagName('item') as $node) {

        $tweet = $node->getElementsByTagName('title')->item(0)->nodeValue;
        if($removeUserID)
        	$tweet = substr($tweet, stripos($tweet, ':') + 1);

        $tweet = preg_replace('@(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)@',
              '<a href="$1">$1</a>', $tweet);

        $tweet = preg_replace('/@([0-9a-zA-Z]+)/',
              '<a href="http://twitter.com/$1">@$1</a>',
              $tweet);

        $outer .= '<li class"tweet">'. $tweet . '</li>';

        if($i++ >= $amount) break;

      }

	$outer .= "</ul>";

    echo $outer;
}

Showing the Tweets

Having created the function, showing the tweets is very easy. The function that you just created can be called as follows:

<?php latest_tweets($username, $amount, $removeUserID); ?>

$username should be your Twitter username.
$amount should be replaced with a number.
$removeUserID can be set to true if you want to remove it or false if you don’t.

To have the function show the three latest tweets from @ErikBernskiold without showing my username for every tweet, this is how the function would look:

<?php latest_tweets('ErikBernskiold', 5, true); ?>

Summing it up

This short and simple function is flexible enough to allow you to implement this wherever you need and in any content management system that you wish to use it in.

What ways are you using to show Twitter on your website? Sound off in the comments!

  • http://www.satinderspace.com/ Satinder Singh

    good one for me as i’m working on customization of twitter follower box

  • Vlad

    This script gives me this error:
    DOMDocument::load(http://twitter.com/statuses/user_timeline/sopovcom.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in

    • http://www.xldstudios.com/ Erik Bernskiöld

      Twitter has, since this post was originally written, changed their API request URL. I have updated the post to reflect this.

      You would want to change the fourth line to read:
      $feed = “http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=$username”;

      • Vlad

        Thank you for quick responce, will check it today

      • Caroline Rodriguez

        I should have posted here :S , as I was saying, Im still getting this error even though url seems to be working. any advice.(note: my allow_url_fopen is turn on.)

    • Caroline Rodriguez

      I got this same error. even with the changes made prior.

  • Zakk

    i am getting ” “. $tweet . ” n ” when i use this script, i am probably doing something wrong because i am pretty new to php, what am i missing?

    • http://www.xldstudios.com/ Erik Bernskiöld

      A coding error got in when I updated the script the other day. On line 25, there were double quotes ” where there needed to be single quotes. I have updated the script in the post to fix this.

  • Chris

    Doesn’t appear that you’ve mentioned this, so I will. Using the API without OAuth and you are ratelimited to 150 queries per hour. This only works on low traffic sites. I’d be a lot more interested in the OAuth version of this script.

    • http://www.xldstudios.com/ Erik Bernskiöld

      True, what is in fact more worrying is that when Twitter finally shuts this API down (which they of course will) the code stops working overall. Twitter seems to want only their official widget to be used nowadays.

      • Chris

        I don’t believe that to be the case. They went through a lot of trouble to incorporate OAuth into the new version of their API (1.1), with the old one just being retired this month. I don’t see them shutting it down entirely.

        I’ve seen a lot of examples of how to post with OAuth, but none on how to create a feed. I operate a relatively busy site and it appears that my only option at the moment is to cache the twitter data I am pulling, but that is less than desirable.

  • http://www.randrcarpetcleaning.com/ R&R Carpet Cleaning

    thanks for the great information