Adding a twitter feed to your website

Fortunately for WebZooki we use the marvellous WooThemes premium WordPress theme ‘Delegate’. This means we simply add our Twitter feed to any of our widget areas at the press of a button. But, don’t worry I’m not getting out of this blog quite that easily!! But almost – I’m a great believer of re-cycling code and so it is with credit to:

http://blog.mivamerchant.com/478/how-to-make-custom-twitter-feed-for-your-website/

and

http://spaceninja.com/2009/07/twitter-php-caching/ (formerly referencing http://stackoverflow.com/questions/11/how-do-i-calculate-relative-time/501415#501415)

…with a few tweaks of my own, that I bring you this blog.

1. Add the following PHP code before the <\body> tag in your footer.php file.

<?php
// Your twitter username.
$username = “vicky_pike”;
// Prefix – some text you want displayed before your latest tweet.
$prefix = “<h2>My last Tweet</h2>”;
// Feed – how you tell Twitter who you are and number of last tweets to take
$feed = “
http://search.twitter.com/search.atom?q=from:” . $username . “&rpp=1″;

echo stripslashes($prefix).”@”.$username;
$twitterFeed = file_get_contents($feed);
// Extract the twitter text from the rest of the info twitter feed
echo parse_feed($twitterFeed,”Feed”);  //See parse_feed function definition below

define(“SECOND”, 1);
define(“MINUTE”, 60 * SECOND);
define(“HOUR”, 60 * MINUTE);
define(“DAY”, 24 * HOUR);
define(“MONTH”, 30 * DAY);

// Extract and echo the published date from the rest of the info in the twitter feed
echo “tweeted “. parse_feed($twitterFeed,”Published”); ?>

2. Add the parse_feed and relativeDate function definitions in the functions.php file.

<?php

//Reformats the tweet’s published date

function relativeTime($time)
{
$delta = strtotime(‘+2 hours’) – $time;
if ($delta < 2 * MINUTE) {
return “1 min ago”;
}
if ($delta < 45 * MINUTE) {
return floor($delta / MINUTE) . ” min ago”;
}
if ($delta < 90 * MINUTE) {
return “1 hour ago”;
}
if ($delta < 24 * HOUR) {
return floor($delta / HOUR) . ” hours ago”;
}
if ($delta < 48 * HOUR) {
return “yesterday”;
}
if ($delta < 30 * DAY) {
return floor($delta / DAY) . ” days ago”;
}
if ($delta < 12 * MONTH) {
$months = floor($delta / DAY / 30);
return $months <= 1 ? “1 month ago” : $months . ” months ago”;
} else {
$years = floor($delta / DAY / 365);
return $years <= 1 ? “1 year ago” : $years . ” years ago”;
}
}
function parse_feed($feed, $which)
{
if ($which == “Published”)
{ $extract_pub_date = explode(“<published>”, $feed);
$extract_pub_date2 = explode(“</published>”,$extract_pub_date[1]);
$relative_time = relativeTime(strtotime($extract_pub_date2[0]));
$published = $relative_time;
return $published;
}
else
{ $extract_entry = explode(“<content type=\”html\”>”, $feed);
$extract_entry2 = explode(“</content>”, $extract_entry[1]);
$tweet = $extract_entry2[0];
$tweet = str_replace(“&amp;apos;”,”‘”, $tweet);
$tweet = str_replace(“&lt;”, “<”, $tweet);
$tweet = str_replace(“&gt;”, “>”, $tweet);
$output = $tweet.$published;
return $output;
}
}
?>

3. The output can then be styled to your liking. My output looked like:

Twitter feed sample

Finally, if you’re after some cool Twitter graphics this site has a great selection: http://siahdesign.com/archives/150 .

And that’s it. Hope it helps!

Tags: , ,

One Comment

Leave a comment
  1. Chris Kirkman 12. Mar, 2010 at 8:53 pm #

    Thank you for referencing my post in the Miva Merchant Blog.

Leave a Reply