TinyURL on every WordPress-post (2)

, under Code snippets.

Just a quick but handy tip I’ve found on this post from smashing magazine.

A lot of people who are using twitter, use tinyURL to create very small links for longer URL’s of blogposts or cool links. Its very powerful solution and cost just a little bit time to create, but you can help ‘the twittering people’ by automatically create the tinyURL-link in your post.
Here’s how you can do this in WordPress. It cost less than 2 minutes too implement 🙂

Just go to design > theme editor > functions.php and add this function
[php]
function getTinyUrl($url) {
$tinyurl = file_get_contents(“http://tinyurl.com/api-create.php?url=”.$url);
return $tinyurl;
}
[/php]Basically you can use this function for every site, but for WordPress it’s handy to use the permalink to create the URL:

In the theme editor > single.php you can add this line where ever you want to show the link to the tidyURL.
[php]ID));
echo ‘Tiny URL: ‘.$turl.’‘;
?>[/php]

2 responses to “TinyURL on every WordPress-post”

  1. Since file_get_contents is synchronic if by any chance tinyurl service is down your blog will stop working correctly.

    If you have implemented it be sure to implement a cache thing and to add validation to see if the url has already been created.

    Cheers!

Say something interesting

Please link to code from an external resource, like gist.github.com.