Shorter Shortlinks – If You Have WWW In Your Domain

Earlier today, I read a post by Stephanie Leary about the shortlink functionality introduced in WordPress 3.0. Afterwards, I felt I should share the code snippet that I use to make my shortlinks even shorter.

Note: This is only for domains that have www in them – for example, I use http://www.scratch99.com/ rather than http://scratch99.com/

I’d like to change to http://scratch99.com/ but I have a lot of links pointing to http://www.scratch99.com/ (365,000 if you believe Yahoo! or almost 2 million if you believe Google Webmaster Tools). While it’s trivial to redirect these links, even a 301 redirect loses some Google juice. Not worth it.

The the_shortlink Function

Stephanie’s post is about the the_shortlink template function. I won’t go into detail about what it does, but basically it creates a shortlink for your post and displays it. This is useful for people wanting to share the link on service such as Twitter, which limits the number of characters you can use.

Many people use Bit.ly or use a solution such as YOURLS by Ozh Richard and Lester Chan to create their own shortener, but the_shortlink function can be an effective alternative.

An Example Shortlink

I use the following code in my single.php:

<?php echo '['; the_shortlink('Shortlink','Shortlink to be used when sharing this article'); echo']';?>
Note: I haven’t quite worked out where to put the Shortlink on my site. Really my site needs a serious redesign!

My post on WordPress caching plugins for shared hosting has a URL of:

http://www.scratch99.com/2010/08/wordpress-caching-plugins-for-shared-hosting/

The extra code creates a shortlink which has a value of:

http://www.scratch99.com/?p=287

These two URLs lead to the same place, but the second one is obviously much shorter.

The Problem

See the www. in that URL? That’s 4 characters that don’t need to be there. That’s right:

http://www.scratch99.com/?p=287

and

http://scratch99.com/?p=287

both lead to the same place. 4 characters may not seem like much, but every character counts on Twitter!

The Solution

Drop the following code in your functions.php file:

add_filter('get_shortlink','sjc_alter_shortlink');
function sjc_alter_shortlink($shortlink) {
	$shortlink = str_replace('www.','',$shortlink);
	return $shortlink;
}

This will filter the shortlink returned by the_shortlink function and run the sjc_alter_shortlink function on it. That’s a very simple function does a replace on the shortlink to remove the www. and return the shorter link.

Note: There are other parameters you can filter such as $id, $context, $allow_slugs, but I’m not sure why you’d need them.

It’s not rocket science, but I hope that snippet helps someone out there.

3 responses on “Shorter Shortlinks – If You Have WWW In Your Domain

  1. Stephen Cronin Post author

    Hi Ozh,

    Thanks for pointing that out. I haven’t actually dug into YOURLS yet although I’ve been meaning to for the longest time.

  2. Jhoecannon

    Stephen,

    Thanks for this info. It may not seem like “rocket science” to you, but to most of us, that’s exactly what it is. So, the info that you’ve provided will be a big help.

    Thanks,
    Jhoe

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Quiz: