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. smart@ss) 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 found a conflict between this plugin and the quoter plugin from www.damagedgoods.it/wp-plugins/quoter/. When both plugins are enabled links that are autolinked inside of quotes made using the “Quote selected text” function break something, it causes the next comment to end up in the spot where the quote should have been.

    After much trial and error I was able to remedy this problem by changing the priorities of both plugins so that autohyperlink goes before quoter.

    The last line of autohyperlink-urls.php becomes:
    add_filter('comment_text', 'c2c_hyperlink_urls', 1);

    and line 129 of quoter.php becomes:
    add_filter('comment_text', 'quoter_addquote', 2);

    Regrettably, I have no idea what caused this to happen, but the above did fix it for me.

    Thanks for the great plugin :)

  2. Plugin results in a duplicate:

    www.doncullen.net/?p=27

    If you go there and look at comment, you’ll see two links in a comment. Both are displayed as duplicates when they aren’t — if you hover the mouse over each link, you can see they go to different addresses. It seems to be taking the first link, and duplicating that as the name for all following links…

  3. You can ignore this. I found the problem of the duplicity, and it wasn’t the plugin. If you took the time to read the previous comment, I apologize for the confusion, and thanks for taking the time to read it! :-)

  4. Pingback: IndoProTech.com

Comments are closed.