Get Facebook Fan Count Using Get_Transient and Wp_Remote_Get


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function get_fan_count(){
  2. $fb_id = '106900272716297';
  3. $count = get_transient('fan_count');
  4. if ($count !== false) return $count;
  5. $count = 0;
  6. $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.'');
  7. if (is_wp_error($data)) {
  8. return 'whoa error!!!';
  9. }else{
  10. $count = strip_tags($data[body]);
  11. }
  12. set_transient('fan_count', $count, 60*60*24); // 24 hour cache
  13. return $count;
  14. }
  15.  
  16.  
  17.  
  18. Add the this into your WordPress theme in the location you wish to display the count.
  19.  
  20.  
  21. <? echo get_fan_count(); ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.