Post Pic

How to: Automatically provide tinyurls for your WordPress blog posts

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!

Related Posts

Related Posts

No related posts.

54 Responses

Feb 14 2009 16:26

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. :)

Feb 14 2009 17:38

Thanks Mike for this useful info :)

Feb 14 2009 17:52

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.

Feb 15 2009 04:10

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!

Feb 22 2009 14:40

thanks for the useful tip..

Feb 22 2009 18:12

thanks,its usefull for me :)

Feb 26 2009 19:15

thanks a lot..!!

Mar 09 2009 19:45

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/

Apr 15 2009 18:08

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

Apr 15 2009 22:54

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.

Apr 15 2009 23:58

@Tschai: Very great trick, I’ll make a recipe with thi sone :)

Apr 19 2009 07:27

Hi there. Just wanted to drop some love. Thanks for sharing this :) .

Apr 20 2009 21:41

Remember that not everyone has access to cURL either. Is there any way that won’t give a PHP warning or uses cURL?

Apr 26 2009 18:43

Thanks for sharing :)

Jun 08 2009 16:07

Thank you very good

Jun 14 2009 14:59

Thanks for the tutorial. How do I do the same with bit.ly links?

Jan 06 2010 18:12

Great tip! I was looking for just this capability.

Jun 12 2010 13:13

Thank You Jean,

I am finding a few recipes of yours useful to my current project, including this one.

Thanks again, Emmy.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required