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!

22 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

  3. Ryan says:

    This is fantastic Tobias.

    I am still trying to get my head around them though.

    I am hoping to be able to get a plugin that will allow me to create a link to Google Maps, taking data from several table rows.

    I currently have:
    id, firstname, lastname, address1, address2, city, postcode, state, country, email, website
    imported from csv.

    In my own custom coded PHP, I simply do:

    echo "&lt;a href=\"inline_map.php?height=300&amp;width=500&amp;AgentsLocation=$row[4]+$row[5]+$row[6]+$row[7]+$row[8]\" class=\"thickbox\" title=\"View on google maps\"&gt;...&lt;/a&gt;";

    (I hope this copies through when I save).

    Is there a way to be able to wrap this sort of output, similar to finding a href in the tables?

    Any response is appreciated…

    • Tobias says:

      Hi Ryan,

      thanks for your comment.

      Your desired feature is not integrated in the plugin natively, but you could write your own extension to achieve something like this.

      It sounds as if you want to use a WP-Table Reloaded table as the data source for that idea, instead of a mySQL table or something similar.

      I recommend taking a look at the code in this comment. There, I present a way to load a table, and pick a single cell from it. You would just need to modify that could a little, and instead of picking a single cell, you would loop through the $table array and can perform your echo there.

      Hope this helps!

      Best wishes,
      Tobias

      • Ryan says:

        Thanks Tobias, that is fantastic.

        I am reviewing your discussion on that page, hopefully I’ll be able to get it to work. If not, this is still awesome.

        Thanks again!

  4. Bill Bennett says:

    I’ve successfully used your extension on a version of part of my “NZ media on Twitter” list.

    The WP-Table Reloaded version is here:
    http://billbennett.co.nz/zealand-media-twitter-table/

    In my original list, I hardcoded the Twitter links into each Twitter name as you can see here:
    http://billbennett.co.nz/new-zealand-media-twitter/

    Would it be possible to encode the addresses in the same way using WP-Table Reloaded and the extension?

    • Tobias says:

      Hi,

      you can achieve this, by modifying the function from the extension to

      function wp_table_reloaded_url_link_converter( $cell_content ) {
          $cell_content = preg_replace("/@(\w+)/", "<a href="http://www.twitter.com/\\1" rel="nofollow">@\\1</a>", $cell_content);
          return make_clickable( $cell_content );
      }

      This should then replace all occurances of “@username” to a link to the Twitter profile.

      Best wishes,
      Tobias

  5. CWH says:

    Does anyone know, if an extension exists to embed the AmazonSimpleAdmin plugin (with [asa]xxxxx[/asa]) within a table cell?

    Very nice plugin overall…
    CWH

    • Tobias says:

      Hi,

      it should be as easy as adding the following line to your theme’s “functions.php” file:

      add_filter( 'wp_table_reloaded_cell_content', array( $asa, 'parseContent' ), 1 );

      Best wishes,
      Tobias

  6. juan says:

    Hi Tobias,

    it seems to be the only comment box available in your blog ;) I have a question about your plugin. I have a CSV with a table, that is updated automaticaly on the server every 15 minutes with the same name. Is there a way to show a WP page with the updated table on this file on a automatically process?

    If not, do you recommend any other plugin or method to do it?

    Thank you.

    • Tobias says:

      Hi,

      there is no other comment form, because you should use the wordpress.org forums for support questions :-) But no problem.

      Unfortunately, what you want to achieve is not possible with my plugin. It will only show static data, it does not have methods to automatically update tables.

      Instead, you should manually code a loading algorithm with PHP, which opens your file, parses it and displays it as a table. That’s how I would do it. I don’t know another plugin that does this though. Sorry.

      Best wishes,
      Tobias

  7. Guillaume says:

    Hi Tobias,

    Thanks for the great plugin! We use it on our site and really like it. The only thing that is not ideal, from our standpoint, is that it puts a lot of CSS code in the main pages where the tables are. Would it be possible to add a feature to the plugin so that the CSS is put in a separate file? If you think this is doable, can you contact me directly. We would be willing to sponsor you for the work to make it happen.
    Thanks a lot!

    Guillaume

  8. Leslie Theophile says:

    Hey, your plugin is fantastic! Just wondering if there is a way to block all the results from showing in the table until a search term is entered. After which, the table would show the entries that match the search term?

    • Tobias says:

      Hi Leslie,

      thanks for your comment.

      Unfortunately, this is not easily possible. It might be with some custom programming towards the DataTables JavaScript library, which is used for the searching, but I have no idea how that would look like. Sorry.

      Regards,
      Tobias

  9. Marmot says:

    I followed the directions to add an extension but am getting a syntax error when I try to activate the plugin. Any suggestions?

    Parse error: syntax error, unexpected T_FUNCTION in /hermes/web03/b487/moo.emersonbirthrxcom/wp-content/plugins/wp-table-reloaded-extensions.php on line 15
  10. Asim says:

    Hi Tobias,

    thanks for sharing such a wonderful plugin.

    Is there any way if I want to generate an automatic link in a new column for each row using any specific cell content in the row?

    Any help would be appreciated…

    • Tobias says:

      Hi Asim,

      thanks for your comment.

      I’m not sure if I understand you correctly, but it sounds as if you want to automatically generate content in all rows once you add a new column? Is that what you mean?

      Unfortunately, something like this is not possible, as the plugin can only work with static content, but it does not really have capabilities to work with dynamic content. Thus, you will have to add the content to the new column manually.

      Regards,
      Tobias

  11. Nadia says:

    Hi Tobias,

    Please help me with simple issue. I want to change the font for my tables, and I placed info into the “Plugin Options”. One table’s font was changed (first one) but the second was not. Please give me directions to follow.

    Thank you! Nadia

    • Tobias says:

      Hi Nadia,

      the CSS in your page looks correct. Unfortunately, I could not find the tables on your site. Can you please provide a link to them?

      Thanks,
      Tobias