Skip to content

Instantly share code, notes, and snippets.

@necolas
Created June 17, 2010 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save necolas/441987 to your computer and use it in GitHub Desktop.
Save necolas/441987 to your computer and use it in GitHub Desktop.
Wordpress custom retweet and save buttons
// Adds custom retweet and delicious buttons to Wordpress template
// Uses bit.ly, delicious, backtype, tweetmeme, and topsy APIs
// Adapted from Backtype Tweetcount plugin
// Nicolas Gallagher / @necolas
function tweet_save($url) {
global $post;
$url = '';
$btype_cnt = null;
$topsy_cnt = null;
$tmeme_cnt = null;
$cnt = null;
// set cache interval
$cache_interval = 60;
// bit.ly API credentials
$bitly_login = "YOURLOGIN";
$bitly_key = "YOURAPIKEY";
if (get_post_status($post->ID) == 'publish') {
$url = get_permalink($post->post_id);
$title = $post->post_title;
if ((function_exists('curl_init') || function_exists('file_get_contents')) && function_exists('json_decode') && function_exists('unserialize')) {
// shorten url
if (get_post_meta($post->ID, 'bitly_short_url', true) == '') {
$short_url = null;
$short_url = shorten_bitly($url, $bitly_key, $bitly_login);
if ($short_url) {
add_post_meta($post->ID, 'bitly_short_url', $short_url);
}
}
else {
$short_url = get_post_meta($post->ID, 'bitly_short_url', true);
}
// backtype data
$btype_meta = get_post_meta($post->ID, 'btype_cache', true);
if ($btype_meta != '') {
$btype_pieces = explode(':', $btype_meta);
$btype_timestamp = (int)$btype_pieces[0];
$btype_cnt = (int)$btype_pieces[1];
}
// expire backtype cache
if ($btype_cnt === null || time() > $btype_timestamp + $cache_interval) {
$btype_response = urlopen('http://backtweets.com/search.php?identifier=bttc&since_id=0&refresh=1&q=' . urlencode($url));
$btype_data = unserialize($btype_response);
if (isset($btype_data['results_count']) && (int)$btype_data['results_count'] >= $btype_cnt) {
$btype_cnt = $btype_data['results_count'];
if ($btype_meta == '') {
add_post_meta($post->ID, 'btype_cache', time() . ':' . $btype_cnt);
} else {
update_post_meta($post->ID, 'btype_cache', time() . ':' . $btype_cnt);
}
}
}
// topsy data
$topsy_meta = get_post_meta($post->ID, 'topsy_cache', true);
if ($topsy_meta != '') {
$topsy_pieces = explode(':', $topsy_meta);
$topsy_timestamp = (int)$topsy_pieces[0];
$topsy_cnt = (int)$topsy_pieces[1];
}
// expire topsy cache
if ($topsy_cnt === null || time() > $topsy_timestamp + $cache_interval) {
$topsy_response = urlopen('http://otter.topsy.com/stats.json?url=' . urlencode($url));
$topsy_data = json_decode($topsy_response, true);
if (isset($topsy_data['response']['all']) && (int)$topsy_data['response']['all'] >= $topsy_cnt) {
$topsy_cnt = $topsy_data['response']['all'];
if ($topsy_meta == '') {
add_post_meta($post->ID, 'topsy_cache', time() . ':' . $topsy_cnt);
} else {
update_post_meta($post->ID, 'topsy_cache', time() . ':' . $topsy_cnt);
}
}
}
// tweetmeme data
$tmeme_meta = get_post_meta($post->ID, 'tmeme_cache', true);
if ($tmeme_meta != '') {
$tmeme_pieces = explode(':', $tmeme_meta);
$tmeme_timestamp = (int)$tmeme_pieces[0];
$tmeme_cnt = (int)$tmeme_pieces[1];
}
// expire tweetmeme cache
if ($tmeme_cnt === null || time() > $tmeme_timestamp + $cache_interval) {
$tmeme_response = urlopen('http://api.tweetmeme.com/url_info.json?url=' . urlencode($url));
$tmeme_data = json_decode($tmeme_response, true);
if (isset($tmeme_data['story']['url_count']) && (int)$tmeme_data['story']['url_count'] >= $tmeme_cnt) {
$tmeme_cnt = $tmeme_data['story']['url_count'];
if ($tmeme_meta == '') {
add_post_meta($post->ID, 'tmeme_cache', time() . ':' . $tmeme_cnt);
} else {
update_post_meta($post->ID, 'tmeme_cache', time() . ':' . $tmeme_cnt);
}
}
}
$cnt = max($btype_cnt, $topsy_cnt, $tmeme_cnt);
}
// retweet
$twitter_status = "RT @necolas: ".$title." - ".$short_url;
$twitter_button = '<a class="social retweet" rel="external nofollow" title="Retweet this article" href="http://twitter.com/home?status='.urlencode($twitter_status).'">'.$cnt.' <span>retweet</span></a>';
echo $twitter_button;
// delicious saves
$saves = saves_delicious($url);
$delicious_button = ' <a class="social delicious" rel="external nofollow" title="Save this article to Delicious" href="http://del.icio.us/post?url='.$url.'">'.$saves.'<span>save</span></a>';
echo $delicious_button;
}
}
// convert file contents into string
function urlopen($url) {
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
} else {
return file_get_contents($url);
}
}
// bit.ly url shortening
function shorten_bitly($url, $bitly_key, $bitly_login) {
if ($bitly_key && function_exists('json_decode')) {
$bitly_url = 'http://api.j.mp/v3/shorten';
$bitly_vars = '?login=' . $bitly_login . '&apiKey=' .$bitly_key . '&longUrl=' . urlencode($url);
$response = urlopen($bitly_url . $bitly_vars);
if ($response) {
$data = json_decode($response, true);
if (isset($data['data']['url'])) {
$link = $data['data']['url'];
}
}
}
return $link;
}
// number of delicious saves
function saves_delicious($url) {
if (function_exists('json_decode')) {
$deli_url = 'http://badges.del.icio.us/feeds/json/url/data';
$deli_vars = '?url=' . urlencode($url);
$response = urlopen($deli_url . $deli_vars);
if ($response) {
$data = json_decode($response, true);
if (isset($data['0']['total_posts'])) {
$num = $data['0']['total_posts'];
}
}
}
return $num;
}
// USE (e.g. in loop in index.php / single.php)
if (function_exists('tweet_save')) {
tweet_save(get_permalink());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment