Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Created April 25, 2012 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wokamoto/2485379 to your computer and use it in GitHub Desktop.
Save wokamoto/2485379 to your computer and use it in GitHub Desktop.
WordPress で記事のツイート数を取得する
<?php
/*
* Get Social Button Count twitter
*/
function get_social_counts( $url ){
$transient_key = md5('get_social_counts-' . $url);
// get cache
if ( false === ($counts = get_transient($transient_key)) ) {
$counts = array();
//twitter tweetcount
$response = wp_remote_get('http://urls.api.twitter.com/1/urls/count.json?url=' . urlencode($url));
if( !is_wp_error( $response ) && $response['response']['code'] === 200 ) {
$decode_tweetcount = json_decode($response['body']);
$twitter_tweetcount =
isset($decode_tweetcount['count'])
? $decode_tweetcount['count']
: 0;
} else {
$twitter_tweetcount = 0;
}
//set array
$counts['twitter'] = $twitter_tweetcount;
// set cache
set_transient($transient_key, $counts, 60*60*1); // expires 60sec * 60min * 1 = 1hour
}
return $counts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment