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("http://tinyurl.com/api-create.php?url=".$url);
return $tinyurl;
}

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

<?php
$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, “http://tinyurl.com/api-create.php?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

Comments

Got something to say?

You must be logged in to post a comment.