Post Pic

Display the total number of your Twitter followers on your WordPress blog

If you’re on Twitter, you probably display the number of your followers on your blog, using the chicklet from TwitterCounter.com. Today, I’m going to show you how to display your followers in full text mode.

The first thing to do is to paste the following php functions on the functions.php file from your WordPress blog theme:

function string_getInsertedString($long_string,$short_string,$is_html=false){
  if($short_string>=strlen($long_string))return false;
  $insertion_length=strlen($long_string)-strlen($short_string);
  for($i=0;$i<strlen($short_string);++$i){
    if($long_string[$i]!=$short_string[$i])break;
  }
  $inserted_string=substr($long_string,$i,$insertion_length);
  if($is_html && $inserted_string[$insertion_length-1]=='<'){
    $inserted_string='<'.substr($inserted_string,0,$insertion_length-1);
  }
  return $inserted_string;
}

function DOMElement_getOuterHTML($document,$element){
  $html=$document->saveHTML();
  $element->parentNode->removeChild($element);
  $html2=$document->saveHTML();
  return string_getInsertedString($html,$html2,true);
}

function getFollowers($username){
  $x = file_get_contents("http://twitter.com/".$username);
  $doc = new DomDocument;
  @$doc->loadHTML($x);
  $ele = $doc->getElementById('follower_count');
  $innerHTML=preg_replace('/^<[^>]*>(.*)<[^>]*>$/',"\\1",DOMElement_getOuterHTML($doc,$ele));
  return $innerHTML;
}

Then, simply paste the following anywhere on your theme files. Just replace my username with yours.

<?php echo getFollowers("catswhocode")." followers"; ?>

Related Posts

Related Posts

No related posts.

41 Responses

Apr 22 2009 11:53

That’s really awesome!
But I will use twitter counter for such things ;)

Apr 22 2009 14:48

Why rip whole profile page instead of using Twitter API? Also I don’t see any caching.

I use snippet from Yoast with API/curl/cache at my blog for this. :)

Apr 22 2009 15:05

@Rarst: never tried the API. Apparently, I have missed something! Thanks for letting me know!

Apr 22 2009 15:20

@Jean

Stop. I have terrible mess of technologies in my head after messing all last day with one site’s internals.

My thought somehow clicked to Feedburner snippet from Yoast. :) )) But I think API and cache question is still valid. :)

Feeling dumb, I’ll go do something web-unrelated.

Apr 22 2009 15:39

@Rarst: I read yoast.com and haven’t saw anything related to my recipe, but I thought I just missed something on there ;)
But you’re right, there’s probably a better way to do this with Twitter API. Maybe for a future recipe?

Apr 22 2009 15:49

@Jean

I feel like my IQ is halved today. Just remembered I had asked Lyndi about that in the past and she had posted about such snippet:
http://nice2all.com/2008/11/12/showing-your-number-of-twitter-followers/

Now I’ll really go find dark corner and disconnect for a while.

Apr 22 2009 15:51

Rarst is right, it’s better to use API rather than fetching the whole page and parse, because design might change.

Apr 22 2009 15:55

@Rarst: Great! I haven’t saw this post before.
@Christina: Yes Rarst is right, but this have nothing to do with the design of the page at all. The code only get a value from a specific page and later display it on the blog.

Apr 23 2009 00:00

At least fetch URLs like http://twitter.com/users/show/ozh.xml, they are 10x lighter.

Apr 24 2009 10:43

So, I need to add the code to function.php anywhere it can? I get an error on function. Perhaps there is one thing I miss.

Apr 30 2009 13:41

I have asked this question in another post

ca I do it while the twitter plugin on?

May 03 2009 09:14

So Twitter is awesome for Taking traffic to you website . It is very
simple to setup and its a fun positive way to keep in contact with
people. To get more followers on twitter check out this amazing
tool.

May 03 2009 20:05

Heavily based on Yoast code for FeedBurner count, comes with caching, uses Twitter API and no requirements (works on my crappy PHP 4).

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

May 11 2009 08:36

my point of view this more useful and impressive than Twitter Counter, helps to increase value of you and blog if having good amount of followers.

Jun 05 2009 22:20

It works, thanks.

Jun 08 2009 07:59

I love this! Been crazy over twitter..

Jun 14 2009 11:02

Nice post,
I think that another way to use this code is transform it on shortcode.

Jun 26 2009 16:46

Thanks for sharing this code. There are better ways but this is also good.

Sep 02 2009 09:01

Cool site, love the info.

Sep 22 2009 04:55

@Rarst the code you posted has a syntax error on line 3

@Jean Thanks for the tutorial, its good to know all your options even if their are clearly better techniques. I believe what another commenter meant by design was if they change the ID from follower_count to followerCount your script will not work as intended.

Sep 22 2009 07:12

@Montana flynn

Code is correct but when I encoded it for posting here minus got swapped for another similar symbol. Re-type and it should work. :)

Nov 08 2009 15:49

Excellent, finally a working version, thanks for the share.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required