Skip to content

Instantly share code, notes, and snippets.

@kevinlisota
Created May 6, 2015 03:59
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 kevinlisota/4c9eb4fdf3eed46c0430 to your computer and use it in GitHub Desktop.
Save kevinlisota/4c9eb4fdf3eed46c0430 to your computer and use it in GitHub Desktop.
get social share counts
$ID = get_the_ID();
$encode_permalink = rawurlencode(get_permalink());
$encode_title = rawurlencode(html_entity_decode(get_the_title(), ENT_COMPAT, 'UTF-8'));
#encode_description = rawurlencode(html_entity_decode(get_the_excerpt(), ENT_COMPAT, 'UTF-8'));
if (has_post_format('status')) {
$featured_image = get_template_directory_uri() . '/assets/img/geekwire-news-brief.png';
} else {
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
$encode_featured_image = rawurlencode(html_entity_decode($featured_image[0], ENT_COMPAT, 'UTF-8'));
}
if (class_exists('CoAuthors_Plus_Social_Pack')) {
$coauthors = get_coauthors( $post->ID );
foreach ( $coauthors as $coauthor ) {
$twitter_handles[] = $coauthor->twitter;
}
if ( is_array($twitter_handles) || !empty($twitter_handles) ) {
$twitter_related = rawurlencode(html_entity_decode(implode(':Author,', $twitter_handles) . ':Author', ENT_COMPAT, 'UTF-8'));
}
$tweet_intent = 'https://twitter.com/intent/tweet?url=' . $encode_permalink . '&via=GeekWire&text=' . $encode_title . '&related=' . $twitter_related;
}
$linkedin_share = 'http://www.linkedin.com/shareArticle?mini=true&url=' . $encode_permalink . '&title=' . $encode_title. '&summary=' . $encode_description . '&source=GeekWire';
$googleplus_share = 'https://plus.google.com/share?url=' . $encode_permalink;
$reddit_share = '//www.reddit.com/submit?url=' . $encode_permalink;
if ($encode_featured_image) {
$pinterest_url = '//www.pinterest.com/pin/create/button/?url=' . $encode_permalink . '&media=' . $encode_featured_image . '&description=' . $encode_title;
}
if (false === (${"fblikes" . $ID} = get_transient( 'fblikes' . $ID))) {
$fburl = 'https://graph.facebook.com/v2.3/?id=' . $encode_permalink . '&key=value&access_token=364309080316986|01ee7916fe94056e7c7eb2aef9fe81bf';
$fbresponse = wp_remote_retrieve_body(wp_remote_get($fburl, array('timeout' => 1)));
${"fblikes" . $ID}=json_decode($fbresponse, true);
if (! is_wp_error($fbresponse)) {
set_transient('fblikes' . $ID, ${"fblikes" . $ID}, 15 * MINUTE_IN_SECONDS);
}
}
if (false === (${"tweets" . $ID} = get_transient( 'tweets' . $ID))) {
$twurl = 'https://cdn.api.twitter.com/1/urls/count.json?url=' . $encode_permalink;
$twresponse = wp_remote_retrieve_body(wp_remote_get($twurl, array('timeout' => 1)));
${"tweets" . $ID} = json_decode($twresponse, true);
if (! is_wp_error($twresponse)) {
set_transient('tweets' . $ID, ${"tweets" . $ID}, 15 * MINUTE_IN_SECONDS);
}
}
if (false === (${"linkedin" . $ID} = get_transient( 'linkedin' . $ID ))) {
$linkedinurl = 'https://www.linkedin.com/countserv/count/share?url=' . $encode_permalink . '&format=json';
$linkedinresponse = wp_remote_retrieve_body(wp_remote_get($linkedinurl, array('timeout' => 1)));
${"linkedin" . $ID} = json_decode($linkedinresponse, true);
if (! is_wp_error($linkedinresponse)) {
set_transient('linkedin' .$ID, ${"linkedin" . $ID}, 15 * MINUTE_IN_SECONDS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment