
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.

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"; ?>
41 Responses
That’s really awesome!
But I will use twitter counter for such things
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.
@Rarst: never tried the API. Apparently, I have missed something! Thanks for letting me know!
@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.
@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?
@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.
Rarst is right, it’s better to use API rather than fetching the whole page and parse, because design might change.
@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.
At least fetch URLs like http://twitter.com/users/show/ozh.xml, they are 10x lighter.
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.
I have asked this question in another post
ca I do it while the twitter plugin on?
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.
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'];
?>
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.
It works, thanks.
I love this! Been crazy over twitter..
Nice post,
I think that another way to use this code is transform it on shortcode.
Thanks for sharing this code. There are better ways but this is also good.
Cool site, love the info.
@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.
@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.
Excellent, finally a working version, thanks for the share.
Trackbacks: