2

We want our visitors to link to us and share our links with others. However, WordPress often generates very long URLs which are difficult to share. I’ll show you how to produce short and unbreakable URLs.

Have a look at this page:

http://wpml.org/documentation/getting-started-guide/language-setup/

The URL takes 68 characters.

This is the Chinese translation:

http://wpml.org/zh-hans/documentation-4/%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/%E8%AF%AD%E8%A8%80%E8%AE%BE%E7%BD%AE/

That’s 114 characters. A bit long for Twitter and will likely break in an email.

Sure, you can use one of many URL shortening services, but do you really need that? Do you really need to proxy your visitors through a different domain for something as simple as this?

Have a look at the bottom of every page in wpml.org. You’ll see it saying ‘short link to this page’.

The length of these links is around 22 characters. A bit less than before.

And, when you click on it, you’ll see the default link to that page. It’s not something that we invented. This is how WordPress works. Each page has an ID. When you link to that ID, you link to the page. WordPress will redirect visitors to the current permalink, so they see the nice ‘SEO friendly’ URLs.

To add to your theme, first, create a function that generates these default links. This function checks on what type of content we are and generates the URL for it.

You can copy this entire code and add it to your site’s functions.php.

function wpml_default_link($anchor){
    global $sitepress;
    $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');
    }elseif(is_tax()){
        $qv = get_query_var('taxonomy') . '=' . get_query_var('term');
    }

    if($qv){
        $link = '<a rel="nofollow" href="' . $sitepress->language_url($sitepress->get_default_language()) . '?' . $qv . '">' . $anchor . '</a>';
    }else{
        $link = '';
    }

    return $link;
}

Next, edit your theme and call this function.

We added it to footer.php:

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

And, that’s all. Now, every page gets this neat short link and invites people to share it.

Go ahead, try it for yourself.

2 Responses to “Short Links to WordPress Content”

    • The PHP at the body of the post defines language_url. If you’re getting a message about that function missing, it means it’s not pasted correctly to your functions.php file.