How to: Automatically provide tinyurls for your WordPress blog posts

February 14, 2009 at 10:37 am

Are you on Twitter? I am. This service make an intensive use of tinyurls in order to reduce the size of urls. Why not provide automatically provide a tinyurl to your readers so they can use it on Tweeter?

To achieve this recipe, simply open your functions.php file and paste the following code:

function getTinyUrl($url) { 
    $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url); 
    return $tinyurl; 
}

On your single.php file, paste the following within the loop:

<?php 
$turl = getTinyUrl(get_permalink($post->ID)); 
echo 'Tiny Url for this post: <a href="'.$turl.'">'.$turl.'</a>'
?>

Great, isn’t it? Have a good week end everyone!