WordPress tutorials: Get Twitter follower count

Wpsnipp – Adding the first section of code to your functions.php & don’t forget to change $name = ‘problogdesign’; to your twitter user name. Then add the second section of code to your template where you wish to display your twitter follower count. Special thanks to Michael Martin of problogdesign.com.

 

 

 

function update_twitter_count() {
$name = 'problogdesign';
$url = 'http://api.twitter.com/1/users/show.xml?screen_name='. $name;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$count = $xml->followers_count;
$count = (float) $count;
$count = number_format($count);
add_option('twitter_followers');
update_option('twitter_followers', $count);
}
function twitter_count() {
echo get_option('tb_twitter_followers');
}
if (!wp_next_scheduled('your_hourly_hook')) {
wp_schedule_event(time(), 'hourly', 'your_hourly_hook');
}
add_action('your_hourly_hook', 'update_twitter_count');

 

<?php
twitter_count();
?>

Be Sociable, Share!

Leave a comment