public
Last active

Wordpress custom retweet and save buttons

  • Download Gist
functions.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
// 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;
}
single.php (within the loop)
1 2 3 4 5
// USE (e.g. in loop in index.php / single.php)
 
if (function_exists('tweet_save')) {
tweet_save(get_permalink());
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.