Shortlinks for posts
Since WordPress 3.0 was launched, I’m sure you’ve noticed the Get Shortlink button below the post title input field in the Add New / Edit Post screen, beside the post’s permalink. (See image below.) It’s a nifty feature made available to us by good old WordPress.org for shortening our posts’ URLs. Clicking on it will give you a wp.me URL which will redirect to your blog post. (This post’s shortlink, for example, is http://wp.me/pqxl6-Y.)
If you promote your blog posts via Twitter or Plurk, you probably use a plugin for auto-posting to those online services. However, if you’re the type who wants to personalize your shout-outs that go with your blog post link — maybe so you can insert hashtags — you’d have to manually add the post URL yourself, don’t you? And if the URL’s too long, you’d have to use a URL shortener like bit.ly or tinyurl.com (or any of the scores of similar services), right?

Not anymore. With wp.me shortlinks, your WordPress blog now has a built-in URL shortener! Don’t you just love WordPress!?!
But wait, there’s more!! (Am I sounding like a TV salesman already?
)
What if you wanted to make the shortlink visible on the blog for everyone to see? That’s exactly what I did with my food blog. See, sometimes I go back to old posts and promote them on Twitter and Facebook. Instead of having to go inside the dashboard and get the shortlink from the Edit Post screen, I can now just go to the blog post (or even the search results page) and copy the shortlink from under the title. Take a look at this screen shot of a couple of front-page posts from Davao Delicious:
Notice the “[shortlink]” text below the titles? By using a WP template tag, I was able to output the shortlink of each post onto various locations of the blog: the main page, single posts, archives, and the search results page. (And my other blogs are sure to follow suit.)
Here’s how to do it. There is just one new template tag involved: the_shortlink(). This tag must be placed inside The Loop, otherwise it won’t work. And, according to the Codex, it will only work on WordPress sites that implement pretty permalinks.
<?php the_shortlink( $text, $title, $before, $after ); ?>
Here’s how my code looks like:
<div class="post-title">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p class="post-details">
Posted on <?php the_time('j F Y'); ?> by <?php the_author_posts_link(); ?>
<?php the_shortlink('shortlink','','[',']'); ?>
</p>
</div>
The first parameter, $text, can be any string that you’d like to be seen by your site’s visitors. The next parameter, $title, is for the tooltip text that will appear on mouse-over. If you leave it blank, the default is the post’s title. The last two, $before and $after are for HTML codes and/or more text that you’d like to appear before and after the shortlink.
All of the parameters are optional. However, leaving the first one blank will result in this text link: “This is the short link.” Kinda fugly if you ask me, so it might be best to supply your own expression.
That’s it! Have fun with the WordPress URL shortener!
N.B.: Each post (and page, including the front page) actually has an assigned wp.me URL. If you view the page source, you’ll see the shortlink in the header. That is, if you’ve activated the WordPress.com Stats plugin and specified that the shortlink be published as meta data.
















Woooooot! Cool! Thanks for the tip! Now I can add a "shortlink" link/button in my single posts… ^_^
Go go go! (^.^)
thank's for this one! found your cool blog on google when I was searching for WordPress themes. am I glad I clicked on your link. your how-to's are so easy to follow for newbies, i'll definately recommend to my webmaster pals. have a nice day!
You're welcome, Johnson. And thanks for subscribing to my blog!