Created
April 12, 2011 16:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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