Adding A Twitter “Following” Ticker To Your Blog

Written by on February 17, 2011 in WordPress Tutorials - No comments

Modern blogs these days plaster how many subscribers or followers they’ve garnered in feeds and lists. This method of displaying a social networking fan-base can become quite overwhelming. So instead, one of the best ways to display a WordPress blog fan base is to include up to date tickers which show a total list of followers.

The code required to add in fan base tickers is quite simple. All it takes is editing the “functions.php” active theme source file and adding the basic command into your template where you would like the ticker displayed.

First we need to tell WordPress how to display the tickers and where to get the information from. In order to do that we need to open the “functions.php” file and find the very last line in the text, which should resemble the following:

?>

After you have successfully located the final text line, input a space above it and copy the following code:

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='saveHTML();
$element->parentNode->removeChild($element);
$html2=$document->saveHTML();
return string_getInsertedString($html,$html2,true);
}
 
function getFollowers($username){
$x = file_get_contents("http://twitter.com/INPUTYOURTWITTERACCOUNTHERE");
$doc = new DomDocument;
@$doc->loadHTML($x);
$ele = $doc->getElementById('follower_count');
$innerHTML=preg_replace('/^]*>(.*)]*>$/',"\1",DOMElement_getOuterHTML($doc,$ele));
return $innerHTML;
}

After you have pasted the above code into your “functions.php” file you will need to change it a bit to reflect your Twitter user account. Notice the “INPUTYOURTWITTERACCOUNTHERE” section? Simply do as it says and input just your twitter account name (not the url). This code above tells WordPress to look up the account entered and display the information we want. The next bit of code we enter into our template will decide where the information above is displayed (header, body, individual posts, etc).

This next bit of code can be entered into any part of your theme where you would like the ticker displayed. Personally I like the ticker displayed in the header of my blog, hence I added it to my “header.php” source file.

<?php echo getFollowers("instantshift")." Followers"; ?>

Once you have pasted the code above into your theme you can save the file (in this case “header.php”) and check out how it looks on your blog!

Now all you need to do is get working on that Twitter following!

About

Briley Kenney is a young freelance writer who enjoys all things electronic and techie related. Currently Briley writes for a plethora of professional websites including Productwiki, Uberoid, The Tech Labs, and of course WPMods. You can find more personal information on Briley's professional portfolio site here.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest