This is going to be a short tutorial post, it’s about shortening your WordPress blog post url for Twitter, but automatically. Widely known short aliases sites like tr.im and tinyurl is good for shortening long URL into cute & short one. Therefore I am going to use them to automatically shortening post url.

To start the tutorial let’s head to functions.php file & use tinyurl

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

Put that code inside your functions.php and then add the following code inside your single.php:

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

Or you want to use tr.im, that’s fine, follow the following steps then:

Open your functions.php file & add the following code:

1
function getTrimUrl($url) { $tinyurl = file_get_contents("http://api.tr.im/api/trim_simple?url=".$url); return $tinyurl; }

And add this code inside your single.php file:

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

I got this tutorial from Wp WebHost in one of his post entitled Five Awesome functions.php Tricks in WordPress

Related posts:

  1. Tutorial To Split WordPress Post Into Separated Pages
  2. Show Most Popular Post In WordPress Without Plugin
  3. Display Related Post In WordPress Without Plugin
  4. Tutorial To Show Ads only on WordPress Homepage
  5. Delete Category From WordPress Default Category Url