Categories
Package/plugin/module WordPress

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 replies on “Plugin: Auto-hyperlink URLs”

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?

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…

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”.

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.

[…] Derzeit arbeite ich gerade an einer Anpassung an dem Plugin ‘Auto-Hyperlink‘. Dieses interessante Plugin verwandelt spezielle Key-Words direkt in Hyperlinks. Allerdinsg ist das englische Original auf die entsprechenden anglo-amerikanischen Links spezialisiert. Aus diesem Grund passe ich dieses Script etwas an. Jeder der Lust hat, kann dieses natürlich entsprechend ergänzen. Ich persönlich finde die Hyperlinks in den Artikeln sehr hilfreich. Der Original-Autor ist natürlich informiert. […]

[…] Google Sitemaps 9 Spam Karma 2 – Reloaded 5 Related Posts 3 WP-ContactForm 6 Email Notification Plugin StatTraq 2 Adsense Deluxe 5 Akismet 4 Ultimate Tag Warrier 3 Feedburner Feed Replacement 3 WP-Amazon 3 Auto hyperlink URLs 2 Page Navigation 2 Angsuman’s Feed Copyrighter 2 WordPress Database Backup 2 IImage Browser 2 Paged Comments 2 Viper’s Plugins Used 2 Popularity Contest 2 WP-Googlestats 2 Subscribe to Comments AJAX Spell Checker Recent Comments Audio Play Rate My Stuff ViewLevel2 evermore PHP Exec Most Wanted Autometa WP-Cron Quicktags WP Adsense No Duplicate Comments Polite-ifier CSS Compress Del.icio.us Bookmark This Flickr Gallery Heavyweight Categories Live Comment Preview Relative Dates RunPHP Extract Terms Terms2tags Force Word Wrapping WP-cache WordPress From/Where WP Hardened Trackback WordPress Hashcash Adsense Logger Bad Behavior Cat 2 Tag CA Chitika Chitika eMiniMalls Code AutoEscape Paged Comment Editing Optimal Title Better Feed Click Counter IP 2 Nation Random Words Transpose e-mail Batch Categories Enhanced View PXS Mail Form SH-Autolink Search Meter Search Pages Search Reloaded Smart Update Pinger Taggerati x-Dashboard CG Powerpack PhotoPress Subscribe Me Secure Image Ak-Bookmark/ak-subscribe Blogs of the Day (couldn’t find URL) Get RSS (couldn’t find URL) […]

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

Hi, and thanks for the great pluging. I found a situation similar to Nat’s (comment 35) but since it’s a bit different I thought I’d tell you.

(div)email@domain.com(/div)<code>

get's changed to

<code>(div)(a href="mailto:macandbac@hotmail.com(/div)
(p)&#8221; title=&#8221;mailto:macandbac@hotmail.com(/div)
(p)&#8220;&gt;email@domain.com(/div)
(p)(/a)(/p)

where, of course, parenthesis represent angle brackets. Thanks again!

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

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.

[…] 1/ l’ordre des menus 2/ logo header ==> voir templates guides 3/ ID 1 not found ==> ok c’est Posts 4/ jouer avec les emails 5/ reconnaissance automatique des liens ==> c’est fait Mon CMS de rêve – interaction email – cache – blog vs pages – automatic link recognition – simple RTE & add images – easy translations & concurrent links – facilement changement de hostname ex: php1.nethence.com => lala.autredomaine.com – recevoir un email lorsqu’un commentaire est ajouté rubyforge.org/projects/ty… WORDPRESS xavier.borderie.net/wp-fr… wordpress-fr.net/ free blog: wordpress.com/ outlook: wordpress.com/tag/outlook… ?? dev.wp-plugins.org/wiki/U… order / sort menu : wordpress.org/support/top… wordpress.org/support/top… Autolink: www.coffee2code.com/archi… www.coffee2code.com/wp-pl… dotclear super thèmes! www.brindilles.net/index…. www.dotclear.net/trac/wik… xoops www.onlamp.com/pub/a/onla… phpwebsite.appstate.edu/ moodle.org/ Plone (heavy as typo3) zope modx drupal e107 BloxPress Theme leita.wordpress.com/tag/i… www.sybase.fr/ […]

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!

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

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.