Skip to content

Instantly share code, notes, and snippets.

@blobaugh
Created October 9, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blobaugh/0d279a05868f10b981fc to your computer and use it in GitHub Desktop.
Save blobaugh/0d279a05868f10b981fc to your computer and use it in GitHub Desktop.
Facebook share count
<?php
function wds_post_facebook_count( $post_id ) {
$api = "https://graph.facebook.com/";
$url = $api . urlencode( get_permalink( $post_id ) );
$response = wp_remote_get( $url );
if( is_wp_error( $response ) ) {
return 0;
}
if( '200' != wp_remote_retrieve_response_code( $response ) ) {
// Bad url
return 0;
}
$body = wp_remote_retrieve_body( $response );
$body = json_decode( $body );
if( isset( $body->shares ) ) {
// Shares will not be set if there are none!
return $body->shares;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment