Subscribe Via RSS

3910 Subscribers

Subscribe Via RSS
Subscribe Via Twitter

Followers

Subscribe Via Twitter
Twitter Followers As Text

January 7th, 2010 in Coding Tutorials by Richard Carpenter

Twitter Followers As Text

1 Star2 Stars3 Stars4 Stars5 Stars13 Votes, Rating: 4.15
Loading ... Loading ...

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 dont 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.

About The Author

About The Author: Richard Carpenter

Hi im Richard Carpenter and im a freelance web and graphics designer from England. I am also a regular Blogger, Tutorial Writer, and owner of Photoshop Plus. You can follow me on twitter HERE. You may also view my portfolio HERE.

 

Richard Carpenter has written 364 posts.

Be Part Of The Community!

Become part of the hv-designs community.

Subscribe Via RSS or Follow Us On Twitter.

Subscribe Via RSS Follow Us On Twitter

36 Responses to “Twitter Followers As Text”

  1. GeorgeOz says:

    Cheers for this pal… ;-)

  2. Rarst says:

    Glad you find my snippet useful. :) btw mine was in turn based on snippet by Yoast (outdated by now) http://yoast.com/feedburner-subscriber-count/

    And minor update since original version – it is better to use WP HTTP API for compatibility reasons:

    wp_remote_retrieve_body(wp_remote_get())

    instead of

    file_get_contents()

  3. Abraham says:

    This is very cool, one problem it doesn’t seem to be working for me. I followed the steps exactly. Can you please help?

    Thanks in advance!

  4. g3niuz says:

    thanks…
    i was lookin for this…

    cheers from germany bro

  5. cristian says:

    Thanks for snippet.

  6. @RARST,

    thanks for the update., much appreciated

  7. Hema says:

    very helpful tut
    thanx

  8. João says:

    Hello Richard, great tutorial, but, here the number of followers don’t displayed :/

  9. upix says:

    fery useful tutorial..thanks for this

  10. hisham says:

    Please Tell How To Slice my web and How Do I Export This In Dreamwaver

  11. Trudie says:

    I follow your steps en try it with xampp on my mac,
    but I get a Fatal error: Call to undefined function get_option()
    how can I remedy this? Is it because I do not use wordpress for my website?
    I hope you can help me

  12. @Trudie:

    The script wont work in xampp unless you enable some of the options, i cant quite remember which ones they were.

    upload it on a live server, should work with no problems

  13. Rarst says:

    @Trudie

    Yes, this snippet is WordPress-specific. get_option() and update_option() are WP functions to store/retrieve values in database – to use as cache in this case.

    You’ll have to make changes to use it with other CMS or plain PHP.

  14. thx “rarst”

  15. João says:

    this ONLY function in wp?

  16. @João :

    yes its only for wordpres

  17. Trudie says:

    @ Richard and Rarst

    Both of you thanks. I’ve tried it on my wordpress and he did it.
    Very useful tutorial!

    Greetings

  18. antisigma says:

    Simply amazing tutorial!
    Have been searching for something like this for a looong time now.

    Great job! Thanks!

  19. Joe says:

    Great little tut. Thanks.

  20. Just want to say your article is striking. The clarity in your post is simply striking and i can take for granted you are an expert on this subject. Well with your permission allow me to grab your rss feed to keep up to date with forthcoming post. Thanks a million and please keep up the ac complished work. Excuse my poor English. English is not my mother tongue.

  21. To make it work for non-wp pages;
    <?php

    $tw = array();
    $tw["twitterfollowerscount"] = 0;
    if($tw['lastcheck'] (.*)

  22. João says:

    how to display in another system? Ex: a little web gallery

  23. Rarst says:

    @Mustafa Namo?lu

    That will work but without any form of caching it will check count on every page load. Increases page latency and can quickly exhaust Twitter API requests limit. Not recommended.

  24. Devilson says:

    Would i be able to integrate this into a sig for a mybb forum?

    And thank you.

  25. vadimon says:

    My counter is not updated. Why?

  26. Webchester says:

    Richard thank you for the excellent chips.

  27. Ashley says:

    It’d be better to use the Twitter API. SimpleXML is an ideal way of achieving this in PHP.

    $username = “The name of your twitter account”;
    if (!$xml=simplexml_load_file(“http://api.twitter.com/1/statuses/followers/”.$username.”.xml”)) //load the XML file with your user info in
    {
    echo “Could not Read XML File”; //Error checking
    }
    else //if the XML file can be accessed
    {
    $followers = $xml->user->followers_count; //get how many followers you have
    echo $followers; //and output it
    }

  28. Thx ashley

  29. Hi would u also have the code for facebook as well

    Regards
    Mike

  30. Ashish says:

    Wow… nice to know, it is this much easy…
    I will request you to show something like this simply for non-wordpress users, as I am not good in PHP.

    Thanks Richard… you rock!!

Leave a Reply