5

Fancy links are great, but they generate huge URLs. These cause problems when you want to send links to others via email, instant message or Twitter.

I’ll show a solution that doesn’t require any plugin, doesn’t go through an external service and doesn’t create any load on your site.

WordPress has default link aliases to every page

Let’s start with an example. This page talks about using Contact Form 7 multilingual:

http://wpml.org/wordpress-translation/translation-for-texts-by-other-plugins-and-themes/how-to-use-contact-form-7-in-multilingual-websites/

Do you think that’s a long URL, check out the Japanese version:

http://wpml.org/ja/%E3%83%9E%E3%83%AB%E3%83%81%E3%83%AA%E3%83%B3%E3%82%AC%E3%83%AB/%E3%81%9D%E3%81%AE%E4%BB%96%E3%81%AE%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%A8%E3%83%86%E3%83%BC%E3%83%9E%E3%81%AB%E3%82%88%E3%82%8B%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E7%BF%BB%E8%A8%B3/contactform7-m17n-steps/

It’s got 306 characters! Doesn’t even fit into two tweets.

But, try these two:

These are actually links to the same pages. There’s no magic here. These are the default-format links to those pages. Every page, post, tag or category can be accessed via its default link as well as its fancy link. It’s built into WordPress and doesn’t rely on anything else.

And, here is the function that produces those links:

function wpml_default_link($anchor){

  $qv = false;

  if(is_single()){
    $qv = 'p=' . get_the_ID();
  }elseif(is_page()){
    $qv = 'page_id=' . get_the_ID();
  }elseif(is_tag()){
    $tag = &get_term(intval( get_query_var('tag_id') ), 'post_tag', OBJECT, 'display');
    $qv = 'tag=' . $tag->slug;
  }elseif(is_category()){
    $qv = 'cat=' . get_query_var('cat');
  }elseif(is_year()){
    $qv = 'year=' . get_query_var('year');
  }elseif(is_month()){
    $qv = 'm=' . get_query_var('year') . sprintf('%02d', get_query_var('monthnum'));
  }elseif(is_day()){
    $qv = 'm=' . get_query_var('year') . sprintf('%02d', get_query_var('monthnum')) . sprintf('%02d',get_query_var('day'));
  }elseif(is_search()){
    $qv = 's=' . get_query_var('s');
  }

  if($qv){
    $link = '<a rel="nofollow" href="' . get_options('home') . '?' . $qv . '">' . $anchor . '</a>';
  }else{
    $link = '';
  }

  return $link;
}

What it does is:

  1. Determines what kind of page you’re looking at now.
  2. Constructs the default URL for that type of page. It’s different for pages, posts, tags, categories and archives.
  3. Creates a link to that URL.

Adding short links to your pages

You can copy the function from above and paste it to your functions.php file. This will make it available to all the files in your theme. Then, add it somewhere. I think that the footer is a good place, but you choose for yourself.

For wpml.org, we’ve added it to footer.php, making it look like this:

default_link

Now, whenever I want to send someone a link to a page on my site, I just use that URL. When visitors come to that URL, they will be redirected by WordPress to the permalink (the fancy URL).

To include in footer, I added this code:

<?php echo(wpml_default_link(__('Short link to this page','yourtheme'))); ?>

Using WPML? It’s already built in!

WPML includes a set of theme helper functions, stored in a file called docs/theme-integration/wpml-integration.php. Just copy that file to your theme’s directory and include it from functions.php. It will already contain a modified version of that function (which also includes the language path), so you can go ahead and use in your theme.