Twitter Followers As Text

After launching the HV-Designs blog i started recieving alot of e-mails asking me how i displayed my twitter count as pure text. In this post il show you how.

 

Quick Note

Now before i start i just want to say “I did not write the code” im about to show you. So don’t credit me for it. The code was actually written by someone called “Rarst“.

Creating The PHP File

Start off by creating a blank PHP file in your favourite code editor, il be using dreamweaver for this tutorial.

Once you’ve created your blank PHP document copy and paste the following lines of code.

<?php
$tw = get_option("twitterfollowerscount");
if ($tw['lastcheck'] < ( mktime() - 3600 ) )
{
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=YOUR-FEED');
if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
$tw['count'] = $match[1];
}
$tw['lastcheck'] = mktime();
update_option("twitterfollowerscount",$tw);
}
echo $tw['count'];
?>

What The Code Does

The first couple of lines grab the twitter count, notice the “mktime 3600″. This means the twitter count will get called every 3600 seconds or every hour instead of calling it on every page load.

The next couple of lines indicate your twitter feed.

$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=YOUR-FEED');

Where it says “YOUR-FEED” you need to put your twitter name, my twitter is “hvdesigns” so id put in “hvdesigns”.

$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=hvdesigns');

Save your file as “twitter.php” inside your website’s directory.

Displaying The Twitter Count

Inside your your wordpress theme just add the following code where you want the numbers to display.

<?php include("twitter.php"); ?>

You can easily change the way the twitter count looks by using CSS. For example my twitter count looks like this.

HTML

<p><span class="twitter-followers"><?php include("twitter.php"); ?></span> Followers</p>

CSS

p {
color:#fff;
}

span.twitter-followers {
font-size: 24px;
vertical-align: middle;
padding-left: 5px;
color: #1e96db;
}

RSS Subscribers

A while back i also written a tutorial on how to do the same thing with your RSS subscribers, you can view the tutorial HERE.