Post Pic

Display number of Facebook fans in full text on your WordPress blog

If you have a Facebook page for your blog, you might want to display how many fans you have. Today’s recipe will help you to get your Facebook fan count, in full text!

Simply paste the following code in any of your theme files, where you want your Facebook fan count to be displayed. Don't forget to add your page ID on line 2!

<?php
	$page_id = "YOUR PAGE-ID";
	$xml = @simplexml_load_file("http://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT%20fan_count%20FROM%20page%20WHERE%20page_id=".$page_id."") or die ("a lot");
	$fans = $xml->page->fan_count;
	echo $fans;
?>

Thanks to WP Snippets for the recipe!

6 Responses

Dec 29 2011 17:18

I think it’s a better way to use the newest graph api like this:
https://graph.facebook.com/PAGE_ID

you get a json response (smaller than xml), so use “json_decode” to get a php Array.

Dec 30 2011 14:49

Probably a much simple and “eyes soothing” code is this –

$data = json_decode(file_get_contents(“https://graph.facebook.com/digimantra”));
echo $data->likes;

Dec 30 2011 15:39

Great little trick, I use a plugin to get text counts for Facebook, Twitter & Rss if I can figure out how to get all three without a plugin then I will be happy. :)

Jan 05 2012 13:20

doesnt work for me

Feb 11 2012 15:24

Very interesting, I will test it on my blog.
Thanks for sharing.

Mar 06 2012 19:38

The code doesn’t seem to work…can someone get in touch with me please through admin@undergroundsource.co.uk i’m in desperate need of help.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox