Back to Top

Automatically Make Links Clickable

Previous Post:

Automatically Make Links Clickable

In case you haven’t noticed yet, a plain link in comment text will be automatically converted to a clickable link. For example http://example.com will be turned into http://example.com, without your visitors having to actually input <a href="http://example.com">A clickable link</a>. Other types of URLs such as ftp://ftp.domain.com or myemail@domain.com are also converted, which is of course very convenient in some cases.

Such things are made possible by a function named make_clickable()1 that can be found in wp-includes/formatting.php. make_clickable() filters the comment_text hook with this:

  1. add_filter( 'comment_text', 'make_clickable',      9 );
add_filter( 'comment_text', 'make_clickable',      9 );

Since it is that simple, let’s try adding the same filter to our post contents and see if it works:

  1. add_filter( 'the_content', 'make_clickable',      12 );
add_filter( 'the_content', 'make_clickable',      12 );

Now try refreshing any singular page to see some magic in action!

The priority of 12 as used above simply tells WordPress to make links clickable for post contents after shortcodes are parsed (which is at priority 11). If you don’t like such behaviour, just change 12 to any number you want. You should take a look at wp-includes/default-filters.php to choose an appropriate priority for make_clickable().

Being that awesome, however, make_clickable() has a limitation, which you can clearly see in this clickable link: http://codex.wordpress.org/Function_Reference/make_clic ... _clickable.

See the full stop punctuation mark also included in the link, thus making it broken? To avoid such behaviour you must always have one space plus another character after a plain link, or in other words, never put a plain link like that at the end of a paragraph. In case you must, just make the link clickable the normal way ;).

Of course if you don’t like your visitors to be able to post links that way you can easily remove the filter using:

  1. remove_filter('comment_text', 'make_clickable', 9);
remove_filter('comment_text', 'make_clickable', 9);

Very straightforward, eh?

References

  1. http://codex.wordpress.org/Function_Reference/make_clic ... _clickable []
Print Article Trackback Trackback to this Article   Subscribe to Comments RSS Subscribe to Comments RSS
 Sponsor   Themes by Elegant Themes - $39 for unlimited access to 80+ WordPress Themes!

17 Opinions for Automatically Make Links Clickable (1 Trackback)

  1. User's Gravatar
    1
    M.K. Safi April 13, 2011 at 11:54 pm – Permalink

    That’s really useful, but the bug that you pointed it out is a real deal breaker. I personally don’t like it when my links are automatically turned into active links, especially when I use example.com for the link.

    By the way, do you use a plugin for the references section within the article, or how do you do it? (Oh, and the option to be notified of follow-up comments would be great!)

    • User's Gravatar
      3
      OddOneOut April 14, 2011 at 2:06 pm – Permalink

      Don’t worry you will be notified automatically (with reply to your comment) ;). About the references, I use a plugin called WP Footnote, for which I wrote a review here, along with some other small-but-cool plugins. Check them out!

      By the way Safi, you look so handsome, haha!

      • User's Gravatar
        5
        M.K. Safi April 14, 2011 at 7:05 pm – Permalink

        Thanks, lol! :oops:

        Oh, and I like that list of small giant plugins. By the way, what operating system do you use and do you write posts directly in WP’s post editor screen? (I use Windows Live Writer)

        • User's Gravatar
          6
          OddOneOut April 14, 2011 at 8:20 pm – Permalink

          I use Windows 7, Ubuntu, OpenSUSE but I’m mainly on Windows 7. And yes I write post directly using the Visual Editor (I like to combine the two), eh… is that a bad thing? :?

          • User's Gravatar
            7
            M.K. Safi April 15, 2011 at 7:59 pm – Permalink

            I couldn’t get used to Visual Editor because it feels a little slow to me. Adding images to posts takes about 10 times longer than with WLW because with WLW, you add images to posts, but they all get uploaded once when you save the draft or publish — you don’t upload images individually like with Visual Editor.

            Also, with WLW, you can set your writing template to look exactly like your post page, so it feels like you’re writing into the front end directly. And of course, it’s a desktop app, so it’s snappier, has all sorts of keyboard shortcuts, has very customizable and smart auto keyword linking and some other things…I love it!

          • User's Gravatar
            8
            M.K. Safi April 15, 2011 at 8:02 pm – Permalink

            Oh, and it also has plugins. Two of my favorite plugins are Dynamic Templates and Paste Code

        • User's Gravatar
          9
          OddOneOut April 16, 2011 at 11:26 am – Permalink

          Damn I can not reply to your last level comment!

          Your 7th comment is a very small nice review there, I think I’m going to switch soon :). Haha now I really think that I need a notification plugin, but I don’t like the one by Mark Jaquith…

  2. User's Gravatar
    2
    S. Georgie April 14, 2011 at 2:24 am – Permalink

    I also agree with the above commenter that such bug is so annoying. Is there a way to fix it?

    • User's Gravatar
      4
      OddOneOut April 14, 2011 at 2:12 pm – Permalink

      @Safi and Georgie: There’s actually a fix for that (which is currently applied to BetterWP.net, the bug you see in my post was done by me, not by the function itself). However, the fix is still very complicated (a lot of regexps!), so I don’t want to post such fix yet.

      We can make the function ignore example.com and yourdomain.com by using a simple strpos function (or you guys can simply omit the http/https, without the schemes plain links will not be made clickable). I will address that issue in the fix ;), so stay tuned!

  3. User's Gravatar
    10
    d4rkcry3r May 1, 2011 at 5:58 pm – Permalink

    Very useful buddy. Thanks for sharing. I also get this problem, sometimes all my post need to identify that link first. But after read your great article, I understand already, thanks again! :)

    • User's Gravatar
      11
      OddOneOut May 1, 2011 at 10:23 pm – Permalink

      You’re welcome!

      Btw, is there any specific reason why you stopped using BWP GXS? Please tell me so I may improve it. Oh, and sorry for spying on your website :P.

      • User's Gravatar
        12
        d4rkcry3r May 1, 2011 at 10:28 pm – Permalink

        Haha, actually I’m used SEO plugin now, so that’s why I’m not use it anymore. I don’t think your plugin have bugs right now, at least for me. Excellent already.. ;)

  4. User's Gravatar
    13
    Kyle G August 24, 2011 at 2:12 am – Permalink

    Hey great post. I have a question, even though you have good information how can you be sure about all this?

  5. User's Gravatar
    14
    Monica October 4, 2012 at 4:31 am – Permalink

    This post is an inspiration for me! Thank you!

  6. User's Gravatar
    15
    Jeffrey November 12, 2012 at 1:20 am – Permalink

    It’s actually a cool and useful piece of information. I am happy that you simply shared this useful info with us. Please keep us informed like this. Thank you for sharing.

  7. User's Gravatar
    17
    marine walk in freezer kits portland oregon September 2, 2014 at 6:39 am – Permalink

    Many anglers avoid the use of whole eggs altogether because they although they contain all kinds
    of essential nutritional components fish are obviously going to recognise them from
    previous egg-containing baits plus eggs affect digestion in various
    ways. For instance at a time when anglers where sticking rigidly to the mantra of 3 to 5 milliliters of flavour, plus 1 to 2 milliliters of intense sweetener to a pound of base
    mix or more, as measured by 4 large eggs or 6 medium eggs as
    a basis of measurement, I tried other approaches and reaped big rewards against much more experienced anglers.
    The exception to this rule is when fishing structure, there will
    always be a calm spot behind structure that
    cat fish rest in and it will be natural for your bait to settle
    in this area.

  1. The Mighty Mo! Design Co. LLC | Auto-Link urls in WordPress Content - The Mighty Mo! Design Co. LLC | 612-293-8629

    […] to BetterWP.net for this […]

Speak Up Your Mind!

An asterisk (*) indicates a required field and must be filled.




  • Web page and e-mail addresses turn into links automatically.
  • Wrap codes in: <code lang=""></code> or <pre lang="" extra="">
  • Lines and paragraphs break automatically.

Next Post: