This post is in response to a suggestion received via Skribit. The question posed was how to show your number of Twitter followers on your blog. The twist to this question was that the number of followers has to be shown in text.
The question is not related to WordPress directly but the world will not end because I go off-topic every now and then.
Twitter, through their API makes a file called followers.xml available. The number of followers for the relevant Twitter account is contained in this file. Getting at this information can be done in numerous ways but I want to mention three of these.
PHP
This is not my code. I discovered this little script written by Steve Nurse.
<?php
# Get Twitter Follwers Images
# By SteveAirCav
# steve.nurse@digital-media-works.co.uk
# http://www.sirius-cybernetics-corporation.co.uk
#
# Requires cURL and SimpleXML!
#
# Fill in your twitter user name and password in these two rows
$username = "username";
$login = "username:password";
$target = "http://twitter.com/statuses/followers/".$username.".xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target);
curl_setopt($ch, CURLOPT_USERPWD, $login);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$gettwit = curl_exec($ch);
$twitters = new SimpleXMLElement($gettwit);
$following = count($twitters);
if ($following>2) {
echo "<p>I have ".$following." followers...</p>";
}
curl_close($ch);
?>
As mentioned in the script you will need cURL and SimpleXML to make this work. The actual script does more than just this as it loops to show the gravatars of all your followers. I left this bit out as it falls outsdie the scope of the suggestion received.
What with the need for a XML Parser and all, this is IMO far too much effort for far too little reward.
Javascript
Scripting this in Javascript is not too difficult but again a lot of overhead will be necessary just to get at a single number.
One can get away from the overhead by making use of Javascript that is hosted on someone else’s server. The following Javascript does the job:
<script type="text/javascript" language="javascript" src="http://twittercounter.com/widget/index.php?username=username"></script>
Again, this is not my code. I found this at TwitterCounter. I have tested this method and it works well.
For both of the mentioned methods you will have to add your own Twitter login details to the script.
Widgets
There are quite a few widgets available that do this job for you but all those I found show the number of followers within a graphic. There are also some nifty WordPress plugins available.
One of the nicer ones I managed to find also comes from TwitterCounter. These guys offer a choice of 4 graphics that you can use. You can either upload a plugin or grab their code and put it straight into your sidebar or wherever you want it. The code without a graphic (shown above) comes off this same page.
I am sure that if one Googles a bit more there are most probably other options available as well. Immaterial of which way you choose to show this information, the biggest problem I see is the fact that Twitter is notorious for being off-line. You will have to keep this in mind.
As a non-Twitter member trying to answer a non-WordPress question, I hope I have provided enough to get you headed in the correct direction if you are interested in something like this.
What about you, do you have a suggestion for future content on this blog. Check out the fancy widget on the sidebar placed there for this very purpose. You would be more than welcome to contact me with any suggestion(s) you might have.
Till next time.








Now we need something for Plurk, which in my opinion, is superior.
I am not sure but if Plurk makes an API available, something similar will be possible.
Just a couple of days ago I signed up for Twitter and I wondered how people got those Twitter widgets. And now.. a couple of days later..here it is.. All the info I needed!
Thanx!
This is not my stuff, someone else thought this all out. I just Googled for it.
This is great one
Thanks, how about leaving your own suggestions for future posts.
For sure I’ll as soon as something pop into my mind. Thanks Lyndi.
That is pretty cool if you have followers on Twitter you want to display.
I joined a while back, but I just don’t get it. It seems that you have to be well known or have a lot of friends and family for it to work for you.
Actually, the whole social networking thing is new for me and very confusing.
And do not forget, it is extremely time consuming.
Twitter and Plurk can become addictive time-wasters! I have to completely log off to get any work done.
I have had a look at Plurk and I must say, I find the horizontal time line quite intriguing. This might just work for me. This kind of time line makes more sense to me than the norm. Maybe that is why I enjoy comment threading on a blog, I reply at the place where the original comment was.
Time is always something which needs to be wisely used. I find that social networking is time consuming and there is a learning curve, which I am not ready to deal with yet.
My goals are to add more content, tweak my SEO to get more traffic through the search engines.
Hopefully I will get around to the full swing social networking, or at least understand it enough to make it work for my benefit.
At present I select only certain ’social sites’. I can afford to do this as I basically just do it for fun. I spend hours on StumbleUpon, even though I do not get much traffic through this. I think I am addicted to it.
cURL and SimpleXML… Ehm, will it work with generic WP or not? Can’t test at moment. Also is that piece of PHP code caching value or pulling it on each page load?
And I think it is perfecly WordPress question, I wasn’t asking how to do it on Drupal or something.
PS add links to post suggested via Skribit under widget, I think it’s helpful for showing system works
The script is not caching at the moment but this would not be impossible. It would take a lot of thinking but it is possible.
I assume you mean a link to the posts already done in response to suggestions. I will look into this.
Although I have a Twitter account and have it set to pull in my blog posts, that’s about the full extent of what I do with Twitter. I keep reading how power users direct traffic to their websites and blogs, but it’s so overwhelming to me with all the other social networking there is and that I do that I end up not Twittering much at all. Of course I don’t update Facebook very often either, so guess I’m in truth a total failure at social networking
All-in-all though, if I HAD any followers, this would be kind of nifty to show — sort of a popularity rating much like showing how many subscribers to your feed you have (and again, I have a feeling for myself that that’s not very many).
Thanks for doing the leg-work and discovering these options! I like having options
ê¿ê
I am afraid I also am not very good at all this ’social’ stuff, I just do not have the time. In a week I finish my present contract and then I am ‘unemployed’ for 3 months until the next contract. Maybe then I will have some time to look into these things.
Wow thanks for the tip!
Hope you can use some of it.
I’d like to know how to start fixing the validation errors on my blog. I checked using that w3c validation site (or whatever its called hehe) and my blog (front page has 181 errors and 5 warnings! GAH!
I’m assuming getting a ‘high score’ on that test isn’t such a good thing…
You are right, this is one time you do not have a high score. I will get back to you via EMail soon.
I have mailed you with my suggestions on how to solve at least the initial round of problems. Please contact me when these have been done and we can have a look at any remaining issues. Happy validating.
Just when I thought I am the only one working while the rest ‘tweet’.
Thanks. I do suppose that there are many ways of doing this and numerous plugins etc. will still appear.