ExternalLinks2

Summary: Add and icon to external links and make them automatically open in a new window
Version:
Prerequisites:
Status:
Maintainer: Pierre Racine
Categories: links

Questions answered by this recipe

  • How can I make all external links open in a new window?
  • How can I add an icon to external links?

Description

The following markup adds an icon for every external links and make them to open in a new window. Add the markup definition to config.php or another local customisation file. You also need to create an icon images ext.gif and put it in a pub/icons/ directory.

This recipe is different from ExternalLinks in that it will not add the icon for internal links inadvertendly written with http://... or https://...

This recipe now detects sites with either http:// or https://

Markup('[[newwinS|','<[[|',"/(?>\\[\\[\\s*(https:\/\/[^|\\]]*)\\s*\\|\\s*)(.*?)\\s*\\]\\]($SuffixPattern)/e",
"MakeLinkNewWin(\$pagename, PSS('$1'), PSS('$2'), PSS('$3'),'$4')");

Markup('[[newwinS','<[[', "/(?>\\[\\[\\s*(https:\/\/.*?)\\s*\\]\\])($SuffixPattern)/e",
"MakeLinkNewWin(\$pagename, PSS('$1'), NULL, '$2')");

Markup('[[newwin|','<[[|',"/(?>\\[\\[\\s*(http:\/\/[^|\\]]*)\\s*\\|\\s*)(.*?)\\s*\\]\\]($SuffixPattern)/e", 
"MakeLinkNewWin(\$pagename, PSS('$1'), PSS('$2'), PSS('$3'),'$4')");

Markup('[[newwin','<[[', "/(?>\\[\\[\\s*(http:\/\/.*?)\\s*\\]\\])($SuffixPattern)/e", 
"MakeLinkNewWin(\$pagename, PSS('$1'), NULL, '$2')");

function MakeLinkNewWin($pagename, $target, $text, $suffix)
{
    global $UrlLinkFmt, $_SERVER, $PubDirUrl;

    $host = 'http://'.$_SERVER['HTTP_HOST'];

    // We do not make the link external if it is internal 
    if (substr_count($target, $host) == 0 && substr_count($target, $host2) == 0 && substr_count($text, "Attach:") == 0)

        $fmt = "%newwin%".$UrlLinkFmt."&nbsp;<img src=".$PubDirUrl."/icons/ext.gif>";

    if (is_null($text))
        return Keep(MakeLink($pagename, PSS($target), NULL, $suffix, $fmt), 'L');
    else 
        return Keep(MakeLink($pagename, PSS($target), PSS($text), $suffix, $fmt), 'L');
}

Get your icon from here: www.stylegala.com/features/bulletmadness/ dead link%

Rename it to be named like this:
ext.gif
and place it in the icons directory.

Add individual icons to external links using Google Favicon service

If you want to have individual icons for external links, then you can use Googles Favicon service. This url will return the domains favicon in given format. If the icon is unknown to Google, then it will display a default icon. All you need to do is change the two lines from the code above where the $fmt variable is set to following lines:

    // We do not make the link external if it is internal 
    if (substr_count($target, $host) == 0 && substr_count($target, $host2) == 0 && substr_count($text, "Attach:") == 0) {
        $urlparts = parse_url($target);
        $hostname = $urlparts['host'];
        $fmt = "%newwin%".$UrlLinkFmt."&nbsp;<img src=\"http://www.google.com/s2/favicons?domain=".$hostname."&.png\">";
    }

Notes

I could not make this to work with bare hyperlink like http://www.site.com/file.pdf

Release Notes

If the recipe has multiple releases, then release notes can be placed here. Note that it's often easier for people to work with "release dates" instead of "version numbers".

See Also

  • ExternalLinks Configure external links to open in a new window, have a "tooltip title", or use other CSS classes
  • Link icons Add icons to your links according to their extensions.

Contributors

Pierre Racine
Russell Montgomery
Daniel Vorhauer

Comments

See discussion at ExternalLinks2-Talk