Skip Navigation

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:

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

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;

    $qv = 'p=' . get_the_ID();
    $qv = 'page_id=' . get_the_ID();
    $tag = &get_term(intval( get_query_var('tag_id') ), 'post_tag', OBJECT, 'display');
    $qv = 'tag=' . $tag->slug;
    $qv = 'cat=' . get_query_var('cat');
    $qv = 'year=' . get_query_var('year');
    $qv = 'm=' . get_query_var('year') . sprintf('%02d', get_query_var('monthnum'));
    $qv = 'm=' . get_query_var('year') . sprintf('%02d', get_query_var('monthnum')) . sprintf('%02d',get_query_var('day'));
    $qv = 's=' . get_query_var('s');

    $link = '<a rel="nofollow" href="' . get_options('home') . '?' . $qv . '">' . $anchor . '</a>';
    $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, we’ve added it to footer.php, making it look like this:


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.

5 Responses to “Your own URL shortener for WordPress”

    • Copy the entire thing and paste it into functions.php, before the closing ?> tag. If there’s no <?php ?>, enter them around that function.

      • I’m talking about docs/theme-integration/wpml-integration.php: do I copy the contents of that to functions.php?

        And then does that file still work with in the footer.

  1. Will this same method be working with the permalinks shown in the addressbar?

    I’d like to have permalinks enabled and have the links that would show up as Chinese text be converted to something legible by machines… Not Chinese, not gibberish, just simple ABC text.

    • Yes, it works just fine for those too. Have a look at this page:

      It points to a page in Chinese with a Chinese URL. You can see that it goes to it just fine although the permlink includes Chinese characters. You can play around the Chinese and Japanese pages in At the bottom of every page there’s a link to ‘Short link to this page’ which shows the default link.