Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.