Saturday 5 October 2013

Add Short URL Below Posts In WordPress Without Plugins

Important: All scripts hosted on widcraft.googlecode.com don't work anymore because Google has blocked that SVN repository.

Previously I shared a post on this blog about adding a Short Link Widget Below Posts In Blogger, which is a great tutorial for Blogger admins. But I never shared anything like that for WordPress blogs. There are a lot of plugins to add this short link widget below your posts, but we're going to do this without any crappy plugin.

Plus, instead of going with one short link service, you can choose between two short links services. Both services are well known, and easy to use. These two short links services are tr.im and TinyURL.

Add Short URL Below Posts In WordPress:

Add one of the following two codes to your functions.php file:

For tr.im:

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

For TinyURL:

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

After adding one of the above codes to your functions.php file, add following code in your single.php file, within the loop:

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

Above code is for a hyperlink style short link. You can also add a text box style widget with the following code:

<?php $turl = getTinyUrl(get_permalink($post->ID)); echo 'Tiny Url for this post: <input readonly="true" type="text" value="'.$turl.'"/>' ?>

Save your files, and That's IT!
Important: Check our new website TricksPanda.com for WordPress tutorials, plugins and more.
 
Powered by Blogger.