ExternalLinks2
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('[[newwin|','<[[|',"/(?>\\[\\[\\s*(https?:\/\/[^|\\]]*)\\s*\\|\\s*)(.*?)\\s*\\]\\]($SuffixPattern)/", "mu_MakeLinkNewWin4"); Markup('[[newwin','<[[', "/(?>\\[\\[\\s*(https?:\/\/.*?)\\s*\\]\\])($SuffixPattern)/", "mu_MakeLinkNewWin2"); function mu_MakeLinkNewWin4($m) { extract($GLOBALS['MarkupToHTML']); return MakeLinkNewWin($pagename, $m[1], $m[2], $m[3], @$m[4]); } function mu_MakeLinkNewWin2($m) { extract($GLOBALS['MarkupToHTML']); return MakeLinkNewWin($pagename, $m[1], null, $m[2]); } function MakeLinkNewWin($pagename, $target, $text, $suffix){ global $UrlLinkFmt, $_SERVER, $PubDirUrl, $UrlScheme; $host = "$UrlScheme://".$_SERVER['HTTP_HOST']; // We do not make the link external if it is internal if (substr_count($target, $host) == 0 && substr_count($text, "Attach:") == 0) $fmt = "%newwin%".$UrlLinkFmt." <img src=".$PubDirUrl."/icons/ext.gif>"; return Keep(MakeLink($pagename, $target, $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." <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
See Also
- Cookbook /
- ExternalLinks Configure external links to open in a new window, have a "tooltip title", or use other CSS classes (Stable)
- ExternalLinksFavicons Display favicons before links to external websites (Beta)
- LinkIcons Add icons to your links according to their extensions.
Contributors
Pierre Racine
Russell Montgomery
Daniel Vorhauer
Comments
See discussion at ExternalLinks2-Talk