Arrow

Shorten URLs in WordPress posts

First published on May 7, 2008

You, or someone whose blog you manage, might have the need to put long URLs in their WordPress posts. Generally speaking, if these are linked URLs, you should simply use some shorter, descriptive link text and keep the long URL in the link tag. For example, use William Hung article that I haven’t actually read instead of http://tvguide.sympatico.msn.ca/Hung+trades+music+for+mathematics/TVNews/Articles/080422_william_hung_teaches_MH.htm?isfa=1.

However, if this is not possible (you want to show the web address or you simply don’t have control over the person posting), you can use a plugin to automatically shorten the display of the URL, without altering the actual link. For example, instead of the painfully long URL above, it would display as http://tvguide.sympatico.msn.c…..htm?isfa=1.

A great plugin called wp-chunk already exists to solve this problem for URLs in comments. You can modify the plugin to apply to posts by either replacing or adding to the bottom “filter” code with this line:

add_filter('the_content', 'make_chunky');

For those who are scared to modify code, here is a modified version of wp-chunk (with permission from its author) for posts. I have successfully tested it with WordPress 2.5:

Download the modified wp-chunk plugin to shorten URLs in WordPress posts

Simply unzip the file to your plugins folder and activate it. I have also included one extra option to add the functionality of automatically turning all non-linked URLs into links. (Look for the $wp_chunk_clickable setting in the plugin file.) For example, with this option enabled, if you were to type http://www.theblog.ca in your post, it would automatically be linked as http://www.theblog.ca.

Arrow

12 Responses to “Shorten URLs in WordPress posts”


  1. Raul says:

    This would only work if it’s a WordPress.org install, right? I’m not self-hosted so it wouldn’t work I assume.

    What I do is usually just link directly with hyperlink. If I must Twitter it, I use Snurl or TinyUrl


  2. Peter says:

    Yes, unfortunately WordPress.com is restrictive on the plugins it allows :(

    Side note: Google AdSense has really failed here. I’m not promoting diet pills on this page.


  3. Soop says:

    Can I use that technique to filter othere areas such as widgets?

    I use the wp-wall addon but his breaks if I add a long URL. I have installed wp-chunk and it works fine on my comments but doesnt work on the wp-wall output.

    Thanks,


  4. Peter says:

    Hi Soop, it can probably be used on other areas, but those areas must have a "hook" so that the function can properly filter the content. You’ll have to dig a bit deeper into the code of that plugin or ask the author of the plugin as to whether there is such a "hook".


  5. kamal says:

    Hi Peter (have u any relation with the peteranswers.com ^o^)
    as always the best plugin, i had two problems, i save one (URL), and the second not yet (title), and pls did you know how to short my titles, cause i use it in a row table, and the long titles break the lines…
    i’d like some thing like this :
    title befor : Hi peter, how are you? fin!
    title after : hi peter, how…
    thanks in advanced.


  6. Peter says:

    Hi kamal, I’m not completely sure what you mean. However, you can shorten any text using a simple function such as this:

    <?php

    function shorten_text($text, $length) {
        $shortened_text = substr($text, 0, $length);
        $shortened_text .= ‘…';
        return $shortened_text;
    }

    $title = ‘Your very long title blah blah blah';
    $shortened_title = shorten_text($title, 15);

    print $shortened_title;
    ?>

    If you don’t want to cut off strings in the middle of a word, there are some examples here: http://www.php.net/substr


  7. kamal says:

    Hi peter,
    thanks for your help, it’s exactly what i need, your my angel ^o^

    _____________
    sorry to be so late to answer and thanks you, but can u pls add the Comments Notifier to ur comment form, i think it wil help us as me to be notified when some one answer.
    thanks for you’r important help and tips.
    _____________


  8. kamal says:

    Hi again :)
    i try to insert the <?php the_title(); ?> by using the
    $title=the_title(”,”,FALSE)
    and
    $title = get_the_title();

    but i have some error as :
    ________________
    Fatal error: Cannot redeclare shorten_text() (previously declared in C:\Program Files\xampp\htdocs\web\cua\wp-content\themes\cua\index.php:33) in C:\Program Files\xampp\htdocs\web\cua\wp-content\themes\cua\index.php on line 33
    ________________

    Thanks


  9. Peter says:

    Hi Kamal, shorten_text should be defined only once, and a good place to put it is your theme’s functions.php file. Once is it defined, if can be called as many times as needed. Another possible cause is that the file with the shorten_text function is included multiple times. Yet another possible cause is that somewhere else there is a different function that uses the same name, and thus you should rename one of them.


  10. kamal says:

    Hi peter,
    thanks for the help, and would like to thanks also "BertrandB21" [Url : http://www.wordpress-fr.net/support/viewtopic.php?pid=99766#p99766] for the code too.
    Thanks a lot.


  11. Hybrid says:

    Hi,

    This is super useful. Thanks for making this available. However I was wondering how to get this to work for a custom field instead of content within a post?

    Thanks for any help.

    Reply from Peter: I’m not sure, but this appears to be a good solution. In the second post on that page, you’ll see some code you could add to your template, which would apply the same filters that are applied to your post body to a custom field.


  12. dainis w michel says:

    wow, this plugin still works!

    not sure if anyone has found an alternative — and i am not sure if an alternative is necessary — but i figured i’d post and say thnx and let ppl know i’m still using this plugin and am happy with it :-)

Speak your mind

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word