Last active
December 23, 2015 01:38
-
-
Save vajrasar/6561316 to your computer and use it in GitHub Desktop.
This snippet fetches number of Facebook Likes and Tweets for a post (wordpress). It can be done via 1000s of plugins available, but via this snippet we get full control over presentation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getShares($atts) { | |
extract(shortcode_atts(array( | |
'url' => '', | |
'p_id' => '', | |
'bordercolor' => '#dddddd', | |
'textcolor' => '#333333', | |
'border ' => '1', | |
'bordertype' => 'solid', | |
'bgcolor' => '#f7f7f7', | |
), $atts)); | |
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url); | |
$json = json_decode($json_string, true); | |
$twitter = intval( $json['count'] ); | |
$json_string = file_get_contents('http://graph.facebook.com/?ids=' . $url); | |
$json = json_decode($json_string, true); | |
$facebook = intval( $json[$url]['shares'] ); | |
$comments_count = wp_count_comments($p_id); | |
if ($twitter != "0") $text .= "<strong>Twitter</strong>: ".$twitter." times "; | |
if ($facebook != "0") $text .= "<strong>Facebook</strong>: ".$facebook." times "; | |
if ($comments_count->approved != "0") $text .= "<strong>".$comments_count->approved."</strong> Comments "; | |
if ($comments_count->approved == "0") $text .= "<strong> Comments </strong>"; | |
$result = "<br><p style=\"padding: 4px 4px 4px 4px; color: $textcolor; border: $bordercolor $border $bordertype; background: $bgcolor\">Shared on the following social networks - ".$text."</p><br>"; | |
set_transient($sharestransient, $result, 60*60*4); | |
update_option($sharestransient, $result); | |
return $result; | |
} | |
add_shortcode('postshare', 'getShares'); | |
add_action( 'genesis_entry_header', 'do_this_p', 7); | |
function do_this_p(){ | |
global $post; | |
$url = get_permalink($post->ID); | |
$p_id = $post->ID; | |
echo do_shortcode('[postshare url="'.$url.'" p_id="'.$p_id.'"]'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment