Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active February 29, 2016 23:01
Show Gist options
  • Save Jany-M/aed654f173bcb0914ac4 to your computer and use it in GitHub Desktop.
Save Jany-M/aed654f173bcb0914ac4 to your computer and use it in GitHub Desktop.
[WordPress] Get Facebook Page Likes and Cache them
<?php
// Get a user Access Token (or Page Token if you're the page admin)
// https://developers.facebook.com/tools/explorer/145634995501895/
function get_fb_likes($what) {
$token = 'your_token_here';
//delete_transient('cached_fb');
if(false === ( $cached_fb_results = get_transient( 'cached_fb' ))) {
$json_url = 'https://graph.facebook.com/v2.5/'.$what.'?fields=likes&access_token='.$token;
$json = file_get_contents($json_url);
if($json) {
$json_output = json_decode($json);
set_transient( 'cached_fb', $json_output, 60*60*12 );
if($json_output->likes){
return $json_output->likes;
}
}
} else {
if($cached_fb_results->likes){
return $cached_fb_results->likes;
}
}
}
// Use where you want like this
$page_id = '54353388793'; // Go to your page, check at the bottom for Facebook Page ID https://www.facebook.com/yourpage/info/?tab=page_info
echo get_fb_likes($page_id);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment