Services like Tweetmeme, Bitly and Facebook provide useful tools to share links on-line. The Tweetmeme button and Facebook Like button are a perfect example of these tools you can easily add to your WordPress Theme using just a bunch of lines of code.
The most interesting feature of these bottons allows you to show how many people are sharing a specific link on Twitter and Facebook, measuring in this way how a certain post is popular over these social networks. Bitly is instead extremely useful to shorten long URLs into shorter URLs and it’s perfect to share short links on Twitter.
Another practical way to retrieve these data is to use the APIs. Here is a collection of three simple PHP code snippets for WordPress to retrieve how many people are sharing your posts on Twitter and Facebook and shorten URLs using the Tweetmeme, Facebook and Bitly APIs.
Twitter and Tweetmeme total retweets
The following function uses the Tweetmeme API to return how many people retweeted a specific link on Twitter. In your WordPress theme add this function into function.php:
function tweetCount($url) {
$content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
$element = new SimpleXmlElement($content);
$retweets = $element->story->url_count;
if($retweets){
echo $retweets;}
else{
echo '0';
}
}
Then open single.php and add this code into the loop to display the number of total retweets for the current post:
<?php tweetCount( get_permalink($post->post_id) ); ?>
URL shortener with Bitly
The following function uses the Bitly API to return a Bitly short URL. To integrate Bitly with your WordPress theme you need a Bitly account and an API key. The only thing you have to do is to add this function into function.php:
function bitly($url) {
$content = file_get_contents("http://api.bit.ly/v3/shorten?login=YOURLOGIN
&apiKey=YOURAPIKEY
&longUrl=".$url."&format=xml");
$element = new SimpleXmlElement($content);
$bitly = $element->data->url;
if($bitly){
echo $bitly;}
else{
echo '0';
}
}
Then substitue YOURLOGIN with your user name and YOURAPIKEY with your API key you can find here if you are already logged into Bitly.
In single.php add this code into the loop to return the shortened URL of the permalink of the current post:
<?php bitly(get_permalink($post->post_id)); ?>
A useful way to use this code snippet is to integrate it with the Twitter status update link:
<a href="http://twitter.com/home?status=<?php the_title();?>
<?php bitly(get_permalink($post->post_id)); ?> RT @woork" target="_blank">
Facebook Like
The following function uses the Facebook API to return how many people liked a specific post on Facebook. Add this function into functions.php:
function fb_like_count($url) {
$content = file_get_contents("http://api.ak.facebook.com/
restserver.php?v=1.0&method=fql.query
&query=select%20url,%20total_count%20
from%20link_stat%20where%20url%20in%20('".$url."')
&format=xml");
$fb_like_count = simplexml_load_string($content);
echo $fb_like_count->link_stat->total_count;
if(is_bool($fb_like_count)){
print '0';}
else{
echo $fb_like_count;
}
}
In single.php add this code into the loop to display the number of people who liked the current post:
<?php fb_like_count(get_permalink($post->post_id)); ?>
That’s it! Do you have any suggestion to improve the code or other interesting “social” snippets to share? Please leave a comment!