
Are you on Twitter? I am. This service make an intensive use of tinyurls in order to reduce the size of urls. Why not provide automatically provide a tinyurl to your readers so they can use it on Tweeter?

Are you on Twitter? I am. This service make an intensive use of tinyurls in order to reduce the size of urls. Why not provide automatically provide a tinyurl to your readers so they can use it on Tweeter?
To achieve this recipe, 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>' ?>
Great, isn't it? Have a good week end everyone!
54 Responses
Hello,
This way have a warning on PHP. 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. Are more complicated, but is enabled on installing the php.
Thanks Mike for this useful info
A very nice trick once again..
Thank you
Hope the API does not create duplicate tiny urls as my twitter plugin for wordpress also created tinyurl links at times.
One of my buddies is always talking about your blog at work – finally came and checked it out today, nice work! I’m subscribing to your rss feed – keep on posting!
thanks for the useful tip..
thanks,its usefull for me
thanks a lot..!!
Very helpful recipe
I modified it so that it works on (mt) via cURL and incorporated the TinyURL into a hyperlink for submission to Twitter that also includes the title of the post:
http://www.bioneural.net/2009/03/09/latest-tweets-using-simplepie/
This works great for creating a link on the post page but how do integrate it with my home built tweet this code so people can click the link and have the tiny url show up with the title post in the Twitter UI? Code below:
Thanks,
Adam
<a href=”http://twitter.com/home?status=I just read ” title=”Send this page to Twitter!” target=”_blank”> Send this post to Twitter
After having being inspired by a comment on your Smasing Post about automatically providing tinyurls I’ve tried and succeeded using WordPress’ default URL’s and it works just the same:
http://aytemir.com/need-a-short-url-service-try-wordpress/
So anybody with not too long URl should realy consider that instead of a third party tool, I think.
@Tschai: Very great trick, I’ll make a recipe with thi sone
Hi there. Just wanted to drop some love. Thanks for sharing this
.
Remember that not everyone has access to cURL either. Is there any way that won’t give a PHP warning or uses cURL?
Thanks for sharing
Thank you very good
Thanks for the tutorial. How do I do the same with bit.ly links?
Great tip! I was looking for just this capability.
Thank You Jean,
I am finding a few recipes of yours useful to my current project, including this one.
Thanks again, Emmy.
Trackbacks: