Display Latest Twitter Status On Your Blog

Here’s a short little script that will display the latest Twitter status on your blog. I pulled the code from CSS-Tricks and modified it a bit. This little piece of code will pull-up the user’s latest Twitter status, time and source. You can also pull up the user’s name, screen name, location, url, followers, friends, favorites and more.

Here’s the snippet of code:

<?php

function getTwitterStatus($userid){

$url = “http://twitter.com/statuses/user_timeline/$userid.xml?count=1″;
$xml = simplexml_load_file($url) or die(“could not connect”);

foreach($xml->status as $status){
echo $status->text;
echo $status->created_at;
echo $status->source;
echo $status->user->name;
echo date(“F j, Y, g:i a”,strtotime($status->user->created_at));
echo $status->user->statuses_count;
echo $status->user->followers_count;
echo $status->user->friends_count;
echo $status->user->favourites_count;
echo $status->user->location;
echo $status->user->time_zone;
echo $status->user->url;
}

}

?>

You can format the output anyway you want it. For readability purposes, I formatted the $status->user->created_at field with the date function. All the other variables are pretty much self-explanatory.

Add the function in ‘functions.php,’ then insert this code anywhere in your WordPress template.

getTwitterStatus(“user”);

  • Twitter
  • Facebook
  • Google Plus
  • Pinterest
  • PDF
  • RSS

3 Comments

  • At 2010.07.14 06:17, Richard said:

    Copied this to my functions.php and now my admin fails to load – help!!!

    • At 2010.07.15 23:31, Ulysses said:

      Richard, Don’t include getTwitterStatus(‘user’) in the functions.php file. Also, make sure to change getTwitterStatus(‘user’) with your own Twitter username. I’ll make a change in the post since it’s a bit confusing.

      • At 2010.07.29 12:52, ulysses.wordpress.com/ said:

        Testing if the OpenId comment is working. You must be logged in to your OpenId account to work.