How To Display Twitter counter with Text in Wordpress

Display Twitter Counter in Text is easily in WordPress with PHP code, all need to do just write down some PHP Script in our wordpress theme, we can write it on sidebar.php, footer.php, header.php or whatever we want to put on.

Write down Display Twitter Counter in Text code below

<?php
$twit =file_get_contents('http://twitter.com/users/show/USERNAME.xml');
$begin = '<followers_count>'; $end = '</followers_count>';
$page = $twit;
$parts = explode($begin,$page);
$page = $parts[1];
$parts = explode($end,$page);
$tcount = $parts[0];
if($tcount == '') { $tcount = '0'; }
echo '<div><strong>'.$tcount.' </strong> Followers</div>';
?>
Replace USERNAME with your own USERNAME, example our Twitter username is @Errorcodexin (you can follow it if you want ;)

Display Twitter Followers in Text using Transients API
Write this code on your functions.php theme files and save it

function get_twitter_count() {
// first check the transient
$count = get_transient('follower_count');
if ($count !== false) return $count;
// no count, so go get it
$count = 0;
$data = wp_remote_get('<a href="http://api.twitter.com/1/users/show.json?screen_name=Errorcodexin" rel="nofollow">http://api.twitter.com/1/users/show.json?screen_name=Errorcodexin</a>');
if (!is_wp_error($data)) {
$value = json_decode($data['body'],true);
$count = $value['followers_count'];
}
// set the cached value
set_transient('follower_count', $count, 60*60); // 1 hour cache
return $count;
}

Please notes, you need to change username on the request, we have put ErrorCodexin, so you need to put your own username. Next, you need to put the code to call this functions on your sidebar.php or whatever you want put the counter.


<?php echo get_twitter_count(); ?>
If you want style the code you can make like this example


<div class="twcounter"><?php echo get_twitter_count(); ?></div>

And you need add some style on your style.css


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