5 Practical Ways to Integrate URL Shortening Services on Your Blog

Pubblicato il December 21, 2010

In recent years, URL shortening services have gained a considerable popularity thanks to the large success of Twitter. These services (such as ThinyURL, Bit.ly, WP.me) provide Twitter friendly short aliases for redirection of long URLs.
A valuable use of these services is their integration with the pages of your WordPress blog so as to give to visitors short URL addresses ready to be shared on the popular microblogging service.
I already illustrated a simple way to integrate Bit.ly with your WordPress theme and return a Bitly short URL. To do that, you need a Bitly account and an API key. Than, you have to modify your WordPress theme and add this function into the function.php file:

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 Twitter’s status update link:

<a href="http://twitter.com/home?status=<?php the_title();?>
<?php bitly(get_permalink($post->post_id)); ?> RT @woork" target="_blank">

Another interesting way to integrate an URL shortening service on your blog is using TinyURL. TinyURL doesn’t require an account and, unlike Bitly, doesn’t have a rate limit in the API calls. David Walsh wrote an interesting post that illustrates how to use the TinyUrl API with PHP. If you want to integrate it with your WordPress theme add this function into function.php:

function thinyURL($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
}

Open single.php and add the following code into the loop to return the shortened URL of the permalink of the current post:

<?php thinyURL(get_permalink($post->post_id)); ?>

If you want to integrate it with a Twitter’s status update link use this code:

<a href="http://twitter.com/home?status=<?php the_title();?>
<?php thinyURL(get_permalink($post->post_id)); ?> RT @woork" target="_blank">

If you want to use a WordPress plugin without coding, I suggest you to take a look at URL Shortener. This plugin allows you to generate shortlinks for post/pages using URL Shortener Services such as Bit.ly, Su.pr, ping.fm, Digg and many others, it’s simple to implement and has many interesting features you can customize as you prefer. Another good choice is GentleSource Short URL. This plugin creates a short URL from the blog post permalink and stores it in the database. It supports lin.io, unrelo.com, bit.ly, u.nu and tinyurl.com.
Before closing a mention to ShortURL a WordPress plugin that allows you to use your blog as your own URL shorterning service by implementing the Short URL Auto-Discovery specification. Your short URLs will be in the form of http://domain/-code.

  • Hector A. Henry S.

    Nice pots, i have a integration on my blog but as usually we are never satisfy whit what we have we whant to improve.

    Nice codes.

    • Antonio Lupetti

      Thanks Hector. I’m using TinyURL in my current WordPress theme. It’s faster than Bit.ly and doesn’t have limit rate for API calls.

  • Hector A. Henry S.

    Antonio its would be cool to be able to use direct the T.co or the Goo.gl .

    The problem whit T.co its that it work only whit twitter, and the best part of Goo.gl its that it integrate whit the other services that might be you have whit google.

    Look on this link a example of all the Url Short http://goo.gl/Bfnp and it was mention in http://goo.gl/NNds have a nice day or nice there in Italy.

    Thank you to answer.

  • Otto

    Probably not a good idea to make your site call bit.ly’s API on every single hit to the page. You’d be better off using a plugin expressly designed for this.

    Specifically, I’m thinking of WP Bit.ly, which is well supported and integrates well with the built-in WordPress Shortlink API.

    Once you have the plugin set up, a simple call to wp_get_shortlink() will automagically return the proper bit.ly shortlink. This also means that the shortlink button in the WordPress post editor page will give you the proper shortlink as well, and the Shortlink dropdown in the new WordPress 3.1 Admin bar will also give you the bit.ly shortlink too.

    Plugins. They’re generally better than trying to roll your own code.

  • DTX Studios

    Great post. Worth sharing. Thanks.

  • Julio's CoRRp!

    Muchas gracias Antonio, me sirvio mucho, saludos desde Colombia.

  • Gabriel

    Nice way to integrate the URL shortening service! However, I believe it can be improved, so it won’t connect through cURL to bit.ly everytime a link is generated. It may slow the speed response of a server with lots of requests (especially if bit.ly will not respond fast all the time).

  • omagus

    nice codes..!
    I just know that TinyUrl faster than bit.ly

  • alaJoAnn

    If you like using SexyBookmarks, it already includes URL shortening options for Twitter.

  • Rolands

    BTW, for bit.ly You cour use &format=txt and then You; ll not nee to parse xml response.
    And then in code (for WordPress)
    if(!is_wp_error($whatsendift) AND $whatsendift['response']['code']==200){$manssaturs = $whatsendift['body'];}
    where $whatsendift = wp_remote_get()

    Hope this will help someone!

  • Rolands

    sorry for mistypes :P

  • Gerry Jeansonne

    Some genuinely nice stuff on this site, I love it.

  • Maddy

    Guys, suggest you to try http://www.cutpit.com, really cool url shortener with N numbers of unique features like tweet, sms email, link management et al, try it, i use that only, its also very reliable.

    Cheers!

    Maddy

  • Oral ÜNAL

    Hello,
    I open another url shortening system, named Togl.me.. We always developing and improving our system.. This is a part of our projects..

    You can use http://togl.me to shorten your long URLs.. And read some documentation from http://blog.togl.me