DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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 Comments

  1. 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.

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

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

  3. 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. đŸ™‚

  4. doesnt work for me

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

  6. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *