Plugin: Auto-hyperlink URLs

This plugin has been updated! Comments to this post are now closed. For the latest download, documentation, and link for posting new comments related to this plugin, visit the plugin’s new homepage at:
coffee2code.com/wp-plugins/auto-hyperlink-urls
Name:
Auto-hyperlink URLs
Author:
Scott Reilly
Version:
2.01
Last updated:
10 October 2004
Description:

Auto-hyperlink text URLs in post content and comment text to the URL they reference. Does NOT try to hyperlink already hyperlinked URLs. Improves upon WordPress’s default make_clickable function, along with adding some configuration options (such as link truncation).

Notes:

This plugin seeks to address certain shortcomings with WordPress’s default auto-hyperlinking function (make_clickable()) (which itself was borrowed from phpBB). This tweaks the pattern matching expressions to prevent inappropriate adjacent characters from becoming part of the link (such as a trailing period when a link ends a sentence, links that are parenthesized or braced, comma-separated, etc) and it prevents invalid text from becoming a mailto: link (i.e. [email protected]) or for invalid URIs (i.e. blah) from becoming links. In addition, this plugin adds configurability to the auto-hyperlinker such that you can configure:

– If you want auto-hyperlinked text URLs to only show the hostname
– If you want auto-hyperlinked text URLs truncated after N characters
– If you want auto-hyperlinked text URLs to open in new browser window or not
– Text to come before and after the link text for truncated links

This plugin will recognize any protocol-specified URI (http|https|ftp|news)://, etc, as well as e-mail addresses. It also adds the new ability to recognize Class B domain references (i.e. “somesite.net” where not just those prepended with “www.”) as valid links (i.e. “wordpress.org” would now get auto-hyperlinked)

Installation:
  1. Download the file autohyperlink-urls.zip and unzip it into your /wp-content/plugins/ directory.
    -OR-
    Copy and paste the the code ( autohyperlink-urls.phps ) into a file called autohyperlink-urls.php, and put that file into your /wp-content/plugins/ directory.
  2. Optional: Modify any configuration options (presented as defaults for the arguments to the function hyperlink_urls())
  3. Activate the plugin from your WordPress admin ‘Plugins’ page.
Functions:
[I’ve defined the $num_posts arg as 2 for all of the examples in order to limit the amount of sample output.]

function c2c_hyperlink_urls ($text, $mode='0', $trunc_before='', $trunc_after='...', $open_in_new_window=true)

  • $text : The text to search for plain-text URLs which should be hyperlinked
  • $mode : How the text part of the newly created hyperlinks should appear. Assume as an example “http://wordpress.org/support/10”
    • 0 = The full text of the URL (ie. “http://wordpress.org/support/10”)
    • 1 = Host-only (ie. “http://wordpress.org”)
    • 11+ = (Any number greater than 11 will result in only that many characters appearing in the link.)
  • $trunc_before : Text/HTML to appear before each link created when $mode is 0 or 11+ and truncation occurs
  • $trunc_after : Text/HTML to appear after each link created when $mode is 0 or 11+ and truncation occurs
  • $open_in_new_window : Boolean value (either ‘true’ or ‘false’) to determine whether all created hyperlinks should open a new window when the link is clicked by a visitor; default is ‘true’.
Tips & Examples:

Example:

“wordpress.org”, appearing un-hyperlinked in post text or comment text, becomes:
<a href=”http://wordpress.org” title=”http://wordpress.org” target=”_blank”>wordpress.org</a>

Assume the following text appears in a post/comment: “http://www.somesite.org/with/a/very/long/url/that/may/break/your/sites/layout”

$mode = 0
=> www.somesite.org/with/a/very/long/url/that/may/break/your/sites/layout

$mode = 1, $trunc_before='[‘, $trunc_after=’]’
=> [http://www.somesite.org]

$mode=25
=> www.somesite.org/w…

Related Links:
  • WordPress Support Forum announcement for Auto-hyperlink URLs
Release Log:
  • 10 Oct 2004 : v2.01 — Fix to once again prevent linking already hyperlinked URL
  • 27 Sep 2004 : v2.0 —
    • Plaintext URLs can now begin, end, or be all of the post and it will get auto-hyperlinked
    • Incorporated some WP1.3 regular expression changes to make_clickable()
    • Added “gov” and “edu” to the list of common domain extensions (for Class B domain support)
    • No longer displays the protocol (the “http://” part) in the displayed link text
    • Dropped support for auto-linking aim: and icq:
    • Prepended function names with “c2c_” to avoid potential future conflict with other plugins or the WordPress core
    • Changed license from BSD-new to MIT
  • 13 Jul 2004 : v1.01 — Slight tweak to prevent blah from becoming a link
  • 13 Jul 2004: v1.0 — Complete rewrite
  • 08 Jul 2004 : v0.9 — Released to the public
Copyright & Disclaimer:

Copyright (c) 2004 by Scott Reilly (aka coffee2code)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Acknowledgements:
This plugin is an improvement over code that originated with phpBB and further modified by WordPress.

136 thoughts on “Plugin: Auto-hyperlink URLs

  1. I’m not sure now if I need this plugin or not – have copied from the .phps, modified (also added the au TLD since I’m in Australia), uploaded, activated, and it’s working and I’m not getting errors.

    However, sometimes I mention former domains of mine and I *don’t* want them linked because either they go nowhere (parked page or DNS error results) or to someone else’s site (if someone else has since registered the domain name). And sometimes I want an unlinked full URL appearing in my text. So what I want now is a way to prepend a “do not link” string in my entry with some kind of unusual string that the plugin will interpret as “do not URLify any characters between here and the next space”.

    Any chance of that?

  2. I’m getting the same problem as Matt Gertler reported last December: if an URL finishes a line, WP adds as which this plugin adds to the URL…

  3. Wonderful — I love your plugins. They are so practical and easy to understand. Most bang per line of code.

    Just installed it on eggcorns.lascribe.net/contribute (400+ commentaries, many with messy URLs).

    Some URLs did get messed up, btw — strange html entities appearing. Obviously some sort of conflict with a formatting function. I fixed it by lowering the “priority” setting in add_filter to “2”.

  4. Hi,
    actually I am working on a german translation (and update) on your plugin. This dedicated german update will have implemented some of the main sites and – in addition – some special unix (Ubuntu) links. If this is ok, please let me know as I would like to link this page on my web-site.

  5. Hi,

    thank you, you’re plugin works very well but there is a little bug when the function who format the link tag is followed by a html tag like li, br, dd etc… then the link appear like this :
    yourlink.com />
    text to continue…
    Is there’s a way to correct this please 🙂 ?
    TIA

  6. This plugin seems incompatible with PHP Markdown. When Markdown is activated Autolink doesn’t work. No bug or strange behaviour, just no clickable links.

  7. Pingback: Glob! »
  8. Found a bug: If you use inline CSS, and have a URL in the CSS, that URL gets linkified. Example:

    <a href=”http://www.somesite.com” style=”background: url(http://www.somesite.com/myimage.png); “>Some Site</a>

    The “http://www.somesite.com/myimage.png” gets turned into a clickable link. It seems your plugin’s regexp should be ignoring everything inside <> tags.

    – Sean

  9. Hello. The auto-linker grabs the URLs within embedded objects (WMV movies for sure, and probably all other object types).

    
    site/file.wmv</a>
    site/file.wmv</a>" /&gt;
    
    

    (Hopefully that sample code will display.)
    Basically, the object’s data= attribute, which is a URL, is getting mangled. Likewise, the param’s value= attribute is also getting linkified. This totally breaks the embedded video object.

    It sounds similar to the problematic URLs in the CSS code mentioned above. Hopefully this is an equally quick fix.

    I will be happy to help you test it if that would help. Until a fix is posted, I guess I will have to disable your plugin. 🙁

    Thanks for creating the plugin, and thanks in advance for helping to fix this bug.

  10. Pingback: elge’s blog
  11. Works great on WP2.0.5!

    I tweaked it to also allow .uk and .jp domains and it works fine.

    However I also using ‘Get Custom Field Values” plugin to display some info within the loop. I have tried to add code in the auto-hyperlink plugin to allow text in my cusotm field values to also be hyperlinked, but I can’t get it to work.

    I am trying to put this at the bottom of the plugin:
    add_filter('c2c_get_custom', 'c2c_hyperlink_urls', 9);

    It doesn’t seem to work… any ideas how I can tweak it to include my output WITHIN the loop from the cutom fields?

    thank you!

  12. Hi:

    I am completely new to WordPress…it is awesome so far…and I am working my way through the documentation. Is there a quick and dirty way to make a comment a hyperlink?

    I don’t see it yet.

    Regards and thanks in advance,

    Tim

  13. List of all country codes:

    biz|com|edu|gov|int|mil|net|org|pro|aero|arpa|coop|info|name|nato|tv|us|br|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|fi|fj|fk|fm|fo|fr|fx|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zr|zw|

Comments are closed.