Cached twitter follower count

Add the first section of code to the functions.php of your wordpress theme. This code will cache your Twitter follower number for 1 hour and display the count within your template in any location you place get_follower_count().

 

 

 

function get_follower_Count(){
$count = get_transient('follower_count');
if ($count !== false) return $count;
$count = 0;
$data = wp_remote_get('http://api.twitter.com/1/users/show.json?screen_name=YOURNAME');
if (!is_wp_error($data)) {
$value = json_decode($data['body'],true);
$count = $value['followers_count'];
}
set_transient('follower_count', $count, 60*60); // 1 hour cache
return $count;
}

 

<?
get_follower_count()
?>

Be Sociable, Share!

Leave a comment