How to Display Facebook, Twitter and RSS Counts in Plain Text

Posted: December 27, 2012 in Uncategorized

How to Display Facebook, Twitter and RSS Counts in Plain Text

You need to edit these files

  • functions.php (All WordPress Themes including Genesis)
  • custom_functions.php (If you are using Thesis Theme)

Hence it is recommended to backup your theme.

Follow these steps to edit files in your theme folder.

  • Login to WordPress and navigate to “Apperance” menu.
  • Using theme Editor, edit functions.php file.
  • Copy and paste below code snippets in functions.php file in your theme folder.
  • If you are using Thesis, then paste code snippets in custom_functions.php file.
  • click on “Update File” button and you are done with coding part.

We thank Dave Clements  for writing these awesome code snippets.

How to Display Facebook Fan Page Likes Count in Plain Text

Here is the code snippet that helps to display your Facebook fan page likes count in plain text. It works based on the Facebook developer API and fetches the fan count instantly. In case of connection time-out or any other problem, an “Error” message will be displayed.

function fb_fanpage_count($fanpage_id) {
$count = get_transient('fan_count');
if ($count !== false) return $count;
$count = 0;
$data = wp_remote_get('http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id='.$fanpage_id.'');
if (is_wp_error($data)) {
return 'Error';
}else{
$count = strip_tags($data[body]);
}
set_transient('fan_count', $count, 60*60*24); // 24 hour cache
echo $count;
}

Display Count using Theme Files or Sidebar widget:

In order to display fan count in theme file or sidebar widget, you need to get facebook fanpage id and replace the below id with yours.

<?php fb_fanpage_count ('180265842076201'); ?>

Copy and paste this small piece of code to theme file or sidebar widget.

How to Display Twitter Followers Count in Plain Text

Here is the code snippet that helps to display your Followers count in plain text. If there is a connection time-out, then “error” message will be displayed. You can also replace “Error” message with approximate twitter followers count.

function twitter_followers_count ($twitter_username) {
$url="http://twitter.com/users/show.xml?screen_name=". $twitter_username;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$tw_fol_count = $xml->followers_count;
if ($tw_fol_count == false) { echo 'Error'; }
else { echo number_format($tw_fol_count); }
}

Display Count using Theme Files or Sidebar widget:

Replace below twitter username with yours.

<?php twitter_followers_count ('wpsquare'); ?>

Now, copy and pase this code snippet to theme file or sidebar widget.

How to Display RSS Subscribers Count in Plain Text

This code snippet works only if the Awareness API is activated in FeedBurner dashboard. It automaticallt fetches the number of RSS subscribers and displays the count in plain text.

function rss_count ($rss_user) {
$rssurl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=". $rss_user;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $rssurl);
$stored = curl_exec($ch);
curl_close($ch);
$grid = new SimpleXMLElement($stored);
$rsscount = $grid->feed->entry['circulation']+0;
return number_format($rsscount);
}
function rss_count_run($feed) {
$rss_subs = rss_count ($feed);
$rss_option = "rss_sub_value";
$rss_subscount = get_option($rss_option);
if (is_null($rss_subs)) { return $rss_subscount; }
else {update_option($rss_option, $rss_subs); return $rss_subs;}
}
function rss_sub_value($feed) {
echo rss_count_run($feed);
}

Display Count using Theme Files or Sidebar widget:

Replace “wpsquare” with your feedburner ID

<?php rss_sub_value('wpsquare'); ?>

Copy and paste this small code snippet to theme file or sidebar widget.

If you don’t want to mess up with these code snippets, simple follow our tutorial on How to Add Social Media Icons to Sidebar of Blog and display social profiles links.

Display all these 3 counts at one place:

<?php fb_fanpage_count ('154784051207520'); ?>
<?php twitter_followers_count ('wpsquare'); ?>
<?php rss_sub_value('wpsquare'); ?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s