Skip to content

How To Add Short URL Below Posts In WordPress

In this tutorial, I’ll show you how to add a short URL below posts in WordPress without using any plugin. This will encourage your visitors to share your posts with the help of easy short URLs. As I said, we’re not going to use any external plugin or would have to host anything on our server.

We will use TinyURL’s API to shorten our URLs on the go. It pretty easy, we just need to place two small snippets on our WordPress theme.

Add following snippet to your current theme’s functions.php file:

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

Now, add following snippet to your single.php file or wherever you want to show your short URL:

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

In addition, you can also use tr.im’s API for this job but putting following code to your functions.php file:

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

1 thought on “How To Add Short URL Below Posts In WordPress”

Leave a Reply

Your email address will not be published. Required fields are marked *