Display the Website's Total Number of Twitter Followers
Display the Website’s Total Number of Twitter Followers
In order to create an air of popularity, it might be a good idea to show how many people follow the blog’s Twitter account. This method could also be used to show how many people follow a specific author’s account, thereby encouraging new visitors to log on to Twitter and do the same. This involves a relatively long function within the theme-specific functions.php file, as well as a small piece of PHP code in any of the template files where followers should be displayed. The function looks like this example:
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;$isaveHTML();
$element->parentNode->removeChild($element);
$html2=$document->saveHTML();
return string_getInsertedString($html,$html2,true);
}
function getTwitterFollowers($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;
}
With this code placed into the theme-specific functions.php file, a username can be supplied by an external line of PHP code in order to return a specific account’s followers. That’s done by placing the following code into one or more of the current theme’s template files:
We have < ?php echo getTwitterFollowers(“YourTwitterUserName”).” followers on Twitter”; ?>.