Display Twitter Followers in Text


/ Published in: PHP
Save to your folder(s)

.twcounter {
background: #89B6F8;
padding: 10px;
color: #194B96;
}


Copy this code and paste it in your HTML
  1. function get_twitter_count() {
  2. // first check the transient
  3. $count = get_transient('follower_count');
  4. if ($count !== false) return $count;
  5.  
  6. // no count, so go get it
  7. $count = 0;
  8. $data = wp_remote_get('<a href="http://api.twitter.com/1/users/show.json?screen_name=WPTricksNet" rel="nofollow">http://api.twitter.com/1/users/show.json?screen_name=WPTricksNet</a>');
  9. if (!is_wp_error($data)) {
  10. $value = json_decode($data['body'],true);
  11. $count = $value['followers_count'];
  12. }
  13. // set the cached value
  14. set_transient('follower_count', $count, 60*60); // 1 hour cache
  15. return $count;
  16. }
  17.  
  18. // display <?php echo get_twitter_count(); ?>
  19. //<div class="twcounter"><?php echo get_twitter_count(); ?></div>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.