Skip to content

WordPress: Add number of Twitter, Facebook and Feedburner followers

Many WordPress themes for bloggers feature number of Twitter, Facebook and FeedBurner followers. It is considered a good practice and represent a value for your readers.

In this quick tip, I am going to show you how easy it is to achieve this. Of course, there will be caching involved, so you don’t end up requesting numbers from these three services on every single request.

All methods used here will be pretty generic, so you can easily use them outside WordPress as a standalone scripts.

Table of Contents

Facebook

Open your theme’s functions.php file and copy this method inside:

[code lang=”php”]
/**
* get Facebook page number of likes
* @param string pageID
* @return int
*/
function getFbCount($pageID)
{
$fans = get_transient(‘cfFbLikes’);
if ($false !== $fans) {
$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$pageID."");
$fans = $xml->page->fan_count;
set_transient(‘cfFbLikes’, $fans, 600);
}
return $fans;
}
[/code]

I will explain: first we are checking if inside WordPress transient cache there is a key for our number of fans.

If not, we are requesting this number from FaceBook and storing it inside cache using WordPress transient API for 600 seconds. At last, we are returning number of fans.

To show number of fans anywhere inside your theme, just use something like this:

[code lang=”php”]
echo getFbCount(ID_OF_YOUR_PAGE_HERE);
[/code]

And that’s it.

Twitter

EDIT: As of 11th of June this method is not working anymore, here is the tutorial for the new Twitter 1.1 API

For Twitter the method is very similar, but this time we are getting JSON object as a response:

[code lang=”php”]
/**
* get count of Twitter followers
* @param string Twitter username
* @return int
*/
function getTwitCount($user=’codeforest’){
$apiurl = "http://api.twitter.com/1/users/show.json?screen_name={$user}";

$transientKey = "cfTwitterFollowers";

$cached = get_transient($transientKey);

if (false !== $cached) {
return $cached;
}

// Request the API data, using the constructed URL
$remote = wp_remote_get(esc_url($apiurl));

// If the API data request results in an error, return
// some number 🙂
if (is_wp_error($remote)) {
return ‘256’;
}
$data = json_decode( $remote[‘body’] );
$output = $data->followers_count;
set_transient($transientKey, $output, 600);

return $output;
}
[/code]

The code is very similar to the one we used for Facebook, except I used a handy little WordPress method called wp_remote_get().

wp_remote_get retrieves a URL using the HTTP GET method, returning results in an array. Results include HTTP headers and content.

Again, to show off your Twitter followers count use:

[code lang=”php”]
echo getTwitCount(‘codeforest’);
[/code]

FeedBurner

FeedBurner API was shut down so the below code does not work anymore, you can see the new Uri.lv follower count here

Getting number of RSS readers is similar to above methods:

[code lang=”php”]
/**
* get RSS readers count
* @param string Feedburner id
* @return int
*/
function getRssCount($feedburnerID)
{
$transientKey = "cfRssFollowers";

$rssCount = get_transient($transientKey);

if (false !== $cached) {
$fbUrl="http://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=" . $feedburnerID;
$data = wp_remote_get($fbUrl);

$grid = new SimpleXMLElement($data[‘body’]);
$rssCount = $grid->feed->entry[‘circulation’];

set_transient(‘cf_rss_followers’, $rssCount, 600);
}

return $rssCount;
}
[/code]

Again, to use this inside your theme, use something like this:

[code lang=”php”]
echo getRssCount(YOUR_FEEDBURNER_ID);
[/code]

That’s it for this quick tip, this method can be used with almost any public API to get simple data out of it, so feel free to experiment.

4 thoughts on “WordPress: Add number of Twitter, Facebook and Feedburner followers”

  1. Feedburner doesn’t work, google have been shutting down feedburner api awareness in October 20, but I wondering how you showing total readers in right widget of your blog? Are you still using feedburner api to make this work?

  2. hi. i would like to ask are you using this hack to show your twitter follower,fb likes and feedburner at your website? in right top of your website i mean..

Comments are closed.