3 Practical WordPress Code Snippets You Probably Must Know

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!

  • tommy

    I probably must ought to know these per chance.

    • Nicola Armellini

      Just in case! :)

    • Chris

      The title should probably be rewritten without the ‘probably’. It would probably make it more forceful. It would probably entice more readers.

      Great article, though!

  • Akshay

    I guess you just reoved the facebook like and tweetmeme buttons!!! from your site.
    [I mean those javascript snippet ones]

  • Aminul Islam Sajib

    I believe using those codes instead of plugins make WordPress run faster.

    • Niccolo Favari

      This is often a matter of debate. If the plugin code is lean, there would be no difference at all. Indeed you can read from the codex:
      “This [functions.php] file basically acts like a plugin, and if it is present in the theme you are using, it is automatically loaded during WordPress initialization (both for admin pages and external pages)”

      So, depending on the specific PHP code you’re writing in your plugins and/or in your functions.php, you could obtain the same exact performances. No difference at all.

      This is just a minor aside to clarify the concept of functions.php vs plugins. Well, at least I tried to…

  • Wayne

    A problem with this post:

    This is all I see for insert into post section.

    post_id)); ?>

    • Antonio Lupetti

      Now it seems ok

  • Bali

    I like facebook code….short and useful…thanks buddy

  • Inwebdeveloper

    i like these function. i will try in my blog

  • MadDrummer

    Nice and clean code snippets…but i can’t understand how the integration in “single.php” works.
    For all three examples i see the same code

    post_id)); ?>

    How this code can invoke the functions created in “functions.php”?

  • Antonio Lupetti

    There was a problem for Internet Explorer. I fixed it!

  • Skeku

    Hello Antonio, one question. Last week I did same functions but I got 403 error getting retweet number. I think is due to the API maximum requests per hour. Do you need some caching system?

    • Antonio Lupetti

      I never had this problem!

  • Gerald

    I really like the facebook snippet!! :)

    For the “URL shortener with Bitly” snippet, it would be nice to save the result into a custom field at first generation so that it wouldn’t need to be fetched from bitly everytime.

  • Brooklyn Car Lawyer

    Excellent post. Thanks for the clean WP code snippets.

  • Jonathan

    Thanks Antonio, I was wondering how you managed to get the Tweet counter to work.

    I came across the Tweetmeme API a while ago while trying to figure out how to implement the same functionality on my own site, but a couple of things in their terms of use put me off:

    1. A 250 calls per 60 minutes limit – this might be the source of the 403 error Skeku encountered – are you using WP Super Cache on your pages (which would probably reduce the number of calls)?

    See http://help.tweetmeme.com/2009/04/07/api-documentation/

    2. Their terms of use for the API say you can’t reproduce their button – not sure if this means fully reproduce it in terms of look and feel, or just functionality. Have you had any contact with them re this?

    I’d love to not have to rely on their JS widget and do it all server-side, but I don’t want to get banned either :)

  • Jose

    Why dont u talk about Custom Post in WP3.0 and XML Sitemap Generator

  • job ballard

    all in one place… nice. i think i may know them…

  • Dian

    Sorry, i just found out this code,
    there’s a lot of tut like this but no one explained it like this simple,
    but i have two question,
    why the tweetCount count is using the permalink not the bit.ly link?
    does the bit.ly link is counted in tweetmeme?

  • wallpapers

    thank you :), great code .

  • riesurya

    simple code as I’m looking for for next themes. what amazing :)