Home / functions.php / Get facebook fan count using get_transient and wp_remote_get
  • How to Get facebook fan count using get_transient and wp_remote_get


    I posted a snippet some time ago to get the number of facebook fans this is an updated version using get_transient. Adding this snippet to the functions.php of your wordpress theme will let you cache and display your Facebook fan count using wp_remote_get. Add the second snippet into your wordpress theme in the location you wish to display the count.

    wordpress snippet : PHP

    function get_fan_count(){
             $fb_id = '106900272716297';
             $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='.$fb_id.'');
       if (is_wp_error($data)) {
             return 'whoa error!!!';
             $count = strip_tags($data[body]);
    set_transient('fan_count', $count, 60*60*24); // 24 hour cache
    return $count;

    wordpress snippet : PHP

    <? echo get_fan_count(); ?>

    ( WordPress codex functions, hooks, in this snippet. )

    query, wp_remote_get, _e, is_wp_error, wp,
  • http://www.facebook.com/towfiqi Towfiq Ahmed

    Awesome! Just what I was looking for…

    • http://wpsnipp.com Kevin Chard

      Cool glad to help!

  • Pingback: Display Number of Twitter, Facebook and RSS Subscribers()

  • rajnish

    didn’t work for me :( anyway thanks for sharing

  • Waqas

    Everytimes it shows 616 counts in echo. My fanpage has 135k fans. I checked the the direct api url. It shows 135967

  • Kristof Devos

    Hi, great stuff! How would ony display the total count (FB + Twitter) using this method?