Dukeo » WordPress » TinyURL Link: How To Automatically Get One For Your Blog Posts

TinyURL Link: How To Automatically Get One For Your Blog Posts

Steven WordPress

Mostly pushed by the rapid growth of Twitter (and its 140 characters limit), url shorteners business has also exploded. There are plenty of reason for you to need some shortened urls for your blog posts… Having shorter links to share on Twitter is only a small example.

In order to automatically generate TinyURL shortlinks, first, you need to add the following code to your functions.php file:

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

The next step is to edit single.php in your theme folder and paste the following code (within the loop):

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

If you enjoyed this article,
Get email updates

Join for Free »