The provided widgets that social networking sites like Twitter make offer for external web sites, are not my favourite – I understand the logic behind them; trying to make something consistent, universal, visually flexible, recognizable, ect.. but some sites integration of the social networking tools, are just horrible -
I believe every site should have a social networking feeds.. it shows you’re an active user and it keep your followers interested – but I also believe it has to look good.
I use Twitter for my networking (because it has relatively no ads), so I’ve written this script to pull tweets from a users RSS feed, then format the tweets any way I want. It’s on my site as well as hey-you.ca in the footers.
(If you can’t find your twitter ID in the source code of your twitter page, get it from this site)
The function can be called from your footer.php or whichever file like so:
<?
echo "<h1>Tweets</h1>" .
yourplugin_twitter_feed('123456789');
?>
In your functions.php file include the following two functions:
/*------------------------------------------------------------------
Name: yourplugin_twitter_feed
Purpose: return list of tweets from twitter user ID
EVERY x seconds we're going to refresh the tweets
stored in wordpress' options
Receive: $id = the twitter user id number
$count = number of tweets to retrieve
$refresh = how often to check for new tweets
Return: html formated string of tweets
------------------------------------------------------------------*/
function yourplugin_twitter_feed( $id = '264304328', $count = 1 , $refresh = 7200 ) {
// if nothing exsists in the database.. setup..
if (!get_option('yourplugin_twitter_expire')) {
add_option( 'yourplugin_twitter_expire', (time()+$refresh));
add_option( 'yourplugin_twitter_tweets', get_tweets($id,$count));
}
// if exsists, but tweets are expired/need refreshing
if (get_option('yourplugin_twitter_expire') < time()) {
$temp_old = get_option('yourplugin_twitter_tweets');
update_option( 'yourplugin_twitter_expire', (time()+$refresh));
$get_tweets = get_tweets($id,$count);
$get_tweets = ($get_tweets) ? $get_tweets : $temp_old; //if error, use old ones..
update_option( 'yourplugin_twitter_tweets', $get_tweets);
}
//return list of tweets
return get_option('yourplugin_twitter_tweets');
}
The function above – when called – checks WordPress’s option for the last tweet stored there. The tweet is stored with an expiration time, if expired, the twitter feed will be updated with the function below -
/*------------------------------------------------------------------
Name: get_tweets
Purpose: gets tweets and returns string
Receive: $id = the twitter user id number
$count = number of tweets to retrieve
Return: html formated string of tweets
------------------------------------------------------------------*/
function get_tweets($id = '264304328', $count = 1) {
//get tweets from RSS
$feed_url = "http://twitter.com/statuses/user_timeline/{$id}.rss";
$ch = @curl_init($feed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = @curl_exec($ch);
curl_close ($ch);
$xml = @ new SimpleXmlElement($data, LIBXML_NOCDATA);
if (is_object($xml)) {
$xml = @$xml->channel;
$xml_out = array();
$xml_out['username'] = str_replace('Twitter / ','',$xml->title);
$i = 0;
foreach ($xml->item as $k => $atweet) {
$title = (string)$xml->item[$i]->title;
$date = (string)$xml->item[$i]->pubDate;
$link = (string)$xml->item[$i]->link;
$xml_out['tweets'][]= array('tweet' => $title, 'date' => $date, 'link' => $link);
$i++;
}
$_SESSION['twitter_xml_out'] = serialize($xml_out);
$xml = $xml_out;
} else {
return false; // ERROR, leave function, use old tweets, try again later
}
//assemble tweets in <ul> list
$i = 1;
$tweets = "<ul class='yourplugin_tweets'>";
$the_tweets = '';
if (is_array($xml['tweets'])) {
foreach ($xml['tweets'] as $atweet) {
if ($i <= $count) {
$the_tweets .= "
<li class='yourplugin_tweet_{$i}'>
<div class='yourplugin_tweet_title'>
<a href='{$atweet['link']}'>".
trim(str_replace($xml['username'].": ",'',$atweet['tweet'])).
"</a>
</div><!--/yourplugin_tweet_title-->
<div class='yourplugin_tweet_date'>
".human_time_diff(strtotime($atweet['date']))." ago.
</div><!--/yourplugin_tweet_date-->
</li><!--/yourplugin_tweet_{$i}-->";
}
$i++;
}
}
$tweets .= $the_tweets."</ul>";
//return false of no tweets where retrieved
return (empty($the_tweets)) ? false : $tweets;
}
I prefer to develop in WordPress, just because it makes my life 100x eaiser, but if you’re developing a php application, or using a different framework, you can just change the add/update/get_option()‘s into $_SESSIONS, $_COOKIES, MySQL queries, or whichever storage method you choose.