Post Pic

How to: Automatically get tr.im urls for your blog posts

Some time ago, I shown you how to automatically get tinyurls for your WordPress blog posts. Today, let’s use tr.im, another (and better) url shortening service.

The first thing to do is to add the following function to your functions.php file:

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

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

<?php
$turl = getTrimUrl(get_permalink($post->ID));
echo 'Short Url for this post: <a href="'.$turl.'">'.$turl.'</a>'
?>

That's all, enjoy tr.im urls!

20 Responses

Apr 24 2009 10:54

What are tr.im URL’s…?

Apr 24 2009 14:11

http://tr.im/ ;) Shorten URL Service

Apr 24 2009 14:28

Hmm…never heard of tr.im before this post. Would it also be possible to combine this recipe with a “Post to Twitter” button?

Thanks for the recipe JBJ!

Apr 24 2009 16:03

Just curious, wouldn’t this create a new tr.im url each time a user loads the page?

Apr 24 2009 16:19

@Leland: Of course this is possible, I think I wrote a recipe like this on Smashing Magazine.

@Christina: Nope, it will only create a tr.im per page.

Apr 24 2009 20:13

Thanks!! I really loved this one. I’m a big fan of tr.im :D

BTW Is there any way to link the automatically generated URLs with a tr.im account, so that I can get detailed statistics for the posts?

Apr 24 2009 20:22

@Sjdvda: Glad to read you enjoyed today’s recipe :)
I don’t know about what you asked, sadly. If I learn more, I’ll let you know :)

Apr 24 2009 23:15

Is it just me, or does it seem wasteful to make an HTTP request to Tr.im on every page load? Wouldn’t it make more sense to do it once, then dump it into post meta?

Apr 26 2009 06:35

It`s very useful on subdomains, nice ;)

Apr 26 2009 18:21

@Leland – there is a plugin that makes a ‘Post to Twitter’ link using a URL shortening service. Here’s one of them: http://wordpress.org/extend/plugins/tweet-this/

I love this and have put it in my Twitter links, which are hand coded :) Made my day!

Apr 27 2009 17:39

It seems to me that doing this would cause two unwanted results.

First, changing all your post title permalinks to tr.im urls is really just throwing away your googlejuice. All your internal link structure is decimated. It still works for a casual browser looking at it with eyeballs, but SEO is gone.

Second, you are betting the farm on tr.im. If they suddenly disappear (I wish them the best, but they say only two things are certain death and taxes) all your permalinks are nuked and even the casual viewer will not be able to navigate your site.

Besides, http://yourdomain/your-post-title (or whatever scheme you use) is RESTful. http://tr.im/jP76 is totally opaque and meaningless to human eyes.

Apr 28 2009 21:42

Cool i haven’t tried ti.im, but trim sounds better to me then tinyurl, and it is even shorter :) ..

Jun 02 2009 11:07

Shortening services are pretty risky. Lots of people just don’t click shortened links on principle. You never know what’s on the other side of that link.
I’ve seen a guy posting shortend links to cp site being watched by FBI as a joke. And the IP of every person who followed the link has been recorded by the feds.

Jul 02 2009 14:53

This approach has a flaw: it queues the tr.im url every time the page is opened.

Please have a look at this plugin – it provides short url caching and users can choose from a couple of URL-Shorteners plus other options.

http://hjacob.com/blog/2009/06/short_url_shortcode_wordpress/

Jul 02 2009 14:55

@Roger Waggener:
i dont agree on the googlejuice – because most url shorteners do a 301 redirect -> the juice gets passed.

[ Sorry for double post]

Jul 03 2009 22:21

@Jean-Baptiste, I think this is a good basic idea and I’m not sure why everyone is so being so critical about it.

For everyone against this concept, I suggest running your own URL shortening service, such as YOURLS or Phurl. That way you can track everything and still be sure your links are working. By using some coding similar to What Jean-Baptiste came up with, you can apply this concept to your own work. Come on guys, be original.

Jul 24 2009 18:39

This seems like a great idea, but since I am a n00b, I do not know what/where “within the loop” is. Can someone explain that to me?

Oh, and also – anyone know what the Twitter widget “Wordpress themselves” use? I am in the process og moving my blog from Wordpress.com to my own server, but can’t seem to find the same twitter widget – wich is sad, because it’s exactly what I need – nothing more, nothing less… /offtopic :)

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required