Skip to content

Instantly share code, notes, and snippets.

@reformatco
Last active September 11, 2022 10:18
Show Gist options
  • Save reformatco/2050560 to your computer and use it in GitHub Desktop.
Save reformatco/2050560 to your computer and use it in GitHub Desktop.
Twitter Feed Function for Wordpress
function getTweets($user, $num = 3) {
//first, get the user's timeline
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$user.json?count=$num");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
if ($json === false) { return false; } //abort on error
//second, convert the resulting json into PHP
$result = json_decode($json);
date_default_timezone_set('Europe/London');
/* Added from Skidoosh at http://www.skidoosh.co.uk/php/create-twitter-like-date-formatted-strings-with-php/ */
function twitter_time_format ($date) {
$blocks = array (
array('year', (3600 * 24 * 365)),
array('month', (3600 * 24 * 30)),
array('week', (3600 * 24 * 7)),
array('day', (3600 * 24)),
array('hour', (3600)),
array('min', (60)),
array('sec', (1))
);
$argtime = strtotime($date);
$nowtime = time();
$diff = $nowtime - $argtime;
$res = array ();
for ($i = 0; $i < count($blocks); $i++) {
$title = $blocks[$i][0];
$calc = $blocks[$i][1];
$units = floor($diff / $calc);
if ($units > 0) {
$res[$title] = $units;
}
}
if (isset($res['year']) && $res['year'] > 0) {
if (isset($res['month']) && $res['month'] > 0 && $res['month'] < 12) {
$format = "About %s %s %s %s ago";
$year_label = $res['year'] > 1 ? 'years' : 'year';
$month_label = $res['month'] > 1 ? 'months' : 'month';
return sprintf($format, $res['year'], $year_label, $res['month'], $month_label);
} else {
$format = "About %s %s ago";
$year_label = $res['year'] > 1 ? 'years' : 'year';
return sprintf($format, $res['year'], $year_label);
}
}
if (isset($res['month']) && $res['month'] > 0) {
if (isset($res['day']) && $res['day'] > 0 && $res['day'] < 31) {
$format = "About %s %s %s %s ago";
$month_label = $res['month'] > 1 ? 'months' : 'month';
$day_label = $res['day'] > 1 ? 'days' : 'day';
return sprintf($format, $res['month'], $month_label, $res['day'], $day_label);
} else {
$format = "About %s %s ago";
$month_label = $res['month'] > 1 ? 'months' : 'month';
return sprintf($format, $res['month'], $month_label);
}
}
if (isset($res['day']) && $res['day'] > 0) {
if ($res['day'] == 1) {
return sprintf("Yesterday at %s", date('h:i a', $argtime));
}
if ($res['day'] <= 7) {
return date("\L\a\s\\t l \a\\t h:i a", $argtime);
}
if ($res['day'] <= 31) {
return date("l \a\\t h:i a", $argtime);
}
}
if (isset($res['hour']) && $res['hour'] > 0) {
if ($res['hour'] > 1) {
return sprintf("About %s hours ago", $res['hour']);
} else {
return "About an hour ago";
}
}
if (isset($res['min']) && $res['min']) {
if ($res['min'] == 1) {
return "About one minute ago";
} else {
return sprintf("About %s minutes ago", $res['min']);
}
}
if (isset ($res['sec']) && $res['sec'] > 0) {
if ($res['sec'] == 1) {
return "One second ago";
} else {
return sprintf("%s seconds ago", $res['sec']);
}
}
}
//third, build up the html output
$s = '';
foreach ($result as $item) {
//handle any special characters
$text = htmlentities($item->text, ENT_QUOTES, 'utf-8');
//build the metadata part
//$meta = date('g:ia M jS', strtotime($item->created_at)) . ' from ' . $item->source;
$meta = twitter_time_format($item->created_at);
//parse the tweet text into html
$text = preg_replace('@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@', '<a href="$1" class="hash">$1</a>', $text);
$text = preg_replace('/@(\w+)/', '<a href="http://twitter.com/$1" class="mention">@$1</a>', $text);
$text = preg_replace('/\s#(\w+)/', ' <a href="http://search.twitter.com/search?q=%23$1" class="hash" rel="nofollow">#$1</a>', $text);
$text = preg_replace('/^#(\w+)/', '<a href="http://search.twitter.com/search?q=%23$1" class="hash" rel="nofollow">#$1</a>', $text);
//assemble everything
$s .= '<p class="tweetc">' . $text . "</p>\n" . '<p class="tweet_time">' . $meta . "</p>\n";
}
return $s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment