Skip to content

Instantly share code, notes, and snippets.

@blobaugh
Created October 9, 2015 14:21
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/b9ad7b563d0e6ad75f25 to your computer and use it in GitHub Desktop.
Save blobaugh/b9ad7b563d0e6ad75f25 to your computer and use it in GitHub Desktop.
LinkedIn Share Count
<?php
function linkedin_share_count( $post_id ) {
$api = 'http://www.linkedin.com/countserv/count/share?format=json&url=';
$api .= urlencode( get_permalink( $post_id ) );
if ( ! ( $count = get_transient( 'linkedin_count' . $post_id ) ) ) {
$response = wp_remote_get( $api );
if( is_wp_error( $response ) ) {
// eat it
return 0;
}
// Should verify the correct json is returned
$json = json_decode( wp_remote_retrieve_body( $response ) );
$count = (int) $json->count;
set_transient( 'linkedin_count' . $post_id, absint( $count ), 30 * MINUTE_IN_SECONDS );
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment