Extension 1: URL to Link Conversion

December 31st, 2009 by Tobias Leave a reply »

Just recently I introduced WP-Table Reloaded Extensions, small code snippets that add further functions to the plugin by using the provided “Plugin Actions” and “Plugin Filters”.

The first such Extension that I’m going to present here, is an Automatic Converter for URLs to Links.

In general, you’ll need to manually enter the HTML for a link (something like <a href="http://www.example.com">My Link</a>) into a cell or use the included Wizard that will ask you for the URL and the link text and then generates the HTML for you. This is my recommended way and for most needs it works fine. It also allows for the most flexibility, because you have all freedom to alter or extend that HTML code as you like, e.g. by adding further attributes or a different link text.
Unfortunately it an be a real hassle and a lot of work, if all you want to achieve is, to make URLs you enter “clickable”, i.e. to make them valid HTML links. That can for example be the case when you import a table from a file, because in most cases URLs are not exported as HTML links by a program but simply as the plain URL in text form.

And this is were this Extension kicks in. When a table is shown in a post or page (by using the corresponding Shortcode in it), the extension will basically loop through all cells of that table and check if there’s an URL (for www, ftp or an email address) somewhere. And if it finds one, it will replace the URL with a complete HTML link to that URL that can be clicked by the visitor. The good thing: If there already was a complete HTML link it will be left intact.

Example:
A cell with the content “http://www.example.com” will automatically be converted to a clickable link: “http://www.example.com“.

And here is the code:

/**
 * Converts URLs (www, ftp, and email) in table cells to full HTML links
 * @author Tobias Baethge
 * @see http://tobias.baethge.com/2009/12/extension-1-url-to-link-conversion/
 */
function wp_table_reloaded_url_link_converter( $cell_content ) {
    return make_clickable( $cell_content );
}
add_filter( 'wp_table_reloaded_cell_content', 'wp_table_reloaded_url_link_converter' );

This code just needs to be copied into the file “wp-table-reloaded-extensions.php”, which needs to be created according to these instructions (after the Plugin Header comment, but before the closing PHP bracket ?>). That’s it! :-)

If you like this series of Extensions, I’m happy about any feedback, and especially about further suggestions!

Bookmark this page on:
  • email
  • PDF
  • Print
  • RSS
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
  • Technorati
  • StumbleUpon
  • Google Bookmarks
  • Reddit
  • MisterWong
  • NewsVine
  • Yigg
  • LinkArena
  • LinkedIn
  • Live
  • MySpace
  • Slashdot
  • Webnews.de
  • Wikio
  • FriendFeed
  • Yahoo! Buzz

4 comments

  1. Lembit says:

    Hi Tobias,

    I hope you would consider publishing a complete list of the filters available in WP-Table Reloaded with short descriptions. This could be a post in th Extension series and/or also included in plugin’s documentation.

    Best,
    Lembit

    • Tobias says:

      Hi Lembit,

      yes, I will publish such a list in the documentation, when version 1.6 is released. The Filters are already in the list, just the descriptions will take some time :-)

      Regards,
      Tobias

  2. drapsag says:

    Nice plugin! I made this extension work, but it will not make links open in a new window. Is the option “open links in new window” in the “Plugin Options” only applicable for original links?
    Can it be done for this extension?

    • Tobias says:

      Hi,

      thanks for your comment!

      You are right, that checkbox is only applicable to links created with the little wizard on the “Edit” screen. It does not influence the automatic conversion from this Extension.
      I checked the WP source code again, unfortunately there’s no easy way to have the necessary code inserted there by an Extension.

      But all in all, I actually recommend to not make links open in a new window by default. This should be the choice of your site visitors, who can easily open the link in a new window/tab in their browsers, if they want to. If they don’t want to, you should not force them.

      Best wishes,
      Tobias

Leave a Reply