Automatically Create Tiny Url for ur Tweets

You have to use a URL shortening services when tweeting URLs because of 140 characters restriction in Twitter. For that purpose you can use tinyurls, because tinyurl provides an API, which takes over this task easily and generates unique tinyurls for your blog posts automatically.

To use tinyurl API, edit your functions.php file and paste the following function code:

  1. function getTinyUrl($url) {
  2. $tinyurl = file_get_contents(“http://tinyurl.com/api-create.php?url=”.$url);
  3. return $tinyurl;
  4. }

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

  1. <?php
  2. $turl = getTinyUrl(get_permalink($post->ID));
  3. echo ‘Tiny Url for this post: <a href=”‘.$turl.‘”>’.$turl.‘</a>’
  4. ?>

Tags: ,