Automatically provide tinyurls for your WordPress posts

February 14, 2009 · Print This Article

If you want to reduce the size of urls, you can use tinyurls, but how to provide a tinyurl to your wordpress blog posts?

Simply open your functions.php file and paste the following code:

function getTinyUrl($url) {
$tinyurl = file_get_contents("".$url);
return $tinyurl;

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

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

In your php.ini your “allow_url_fopen” must be enabled. In some servers we can not modify our php.ini. So you can do it using curl.

$ch = curl_init( );
curl_setopt( $ch, CURLOPT_URL, “”.$url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$tinyurl = curl_exec( );

To use this method, the curl must be enabled in your php.ini and instaled. But, in the most shared servers, this option is enabled by default. Otherside you can do the same example using sockets.

Your choice for site templates and wordpress themes


Got something to say?

You must be logged in to post a comment.