Skip to content

Instantly share code, notes, and snippets.

@patrickarlt
Created April 12, 2011 16:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickarlt/915809 to your computer and use it in GitHub Desktop.
Save patrickarlt/915809 to your computer and use it in GitHub Desktop.
A dead simple wordpress plugin to get your tweets on a wordpress site.
<?php
/*
Plugin Name: Awesome Twitter Plugin
Plugin URI:
Description: Adds a function to grab your latest tweets and caches them in the database.
Author: Patrick Arlt
Version: 0.01
Author URI: http://patrickarlt.com
*/
/*
Useage
<?php tweetStream('patrickarlt', 3); ?>
*/
//http://saturnboy.com/2010/02/parsing-twitter-with-regexp/
function parse_tweet($t) {
// link URLs
$t = preg_replace('@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@', '<a href="$1">$1</a>', $t);
//link twitter users
$t = preg_replace('/@(\w+)/', '<a href="http://twitter.com/$1">@$1</a>',$t);
//link twitter arguments
$t = preg_replace('/\s+#(\w+)/',' <a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $t);
return trim($t);
}
function tweetStream($username, $limit){
if (false === ($raw_tweets = get_transient('awesome_twitter'))){
$raw_tweets = wp_remote_fopen('http://api.twitter.com/1/statuses/user_timeline/'.$username.'.json?include_rts=1');
set_transient('awesome_twitter', $raw_tweets, 1);
}
$tweets = json_decode($raw_tweets);
echo('<ul>');
for($i = 0; $i <= ($limit - 1); $i++){
echo("<li>".parse_tweet($tweets[$i]->text)."<time datetime='".$tweets[$i]->created_at."'> ".human_time_diff(strtotime($tweets[$i]->created_at))." ago</time></li>");
}
echo('</ul>');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment