Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

ExternalLinks

Summary: Configure external links to open in a new window, have a "tooltip title", or use other CSS classes
Version:
Prerequisites: 2.0
Status: Stable
Maintainer: Pm
Categories: Links

Questions answered by this recipe

  • How can I make all external links open in a new window?
  • How can I include a title in an external link that will show up as a tooltip?
  • How can I add an icon to external links?
  • How can I change the CSS-class of links to attachments, links to pages on the same site (but not on the wiki), links to other groups?

Description

The $UrlLinkFmt variable specifies the HTML to be output when an external link is generated by PmWiki. The following line in local/config.php (or a local customization file) adds target='_blank' to the HTML so that each external link is opened in a new window.

$UrlLinkFmt = "<a class='urllink' href='\$LinkUrl' rel='nofollow'
  target='_blank'>\$LinkText</a>";

To disable this behavior for specific links, use %target=_self% in front of the link to cause it to open in the current window.

To include a "tooltip title" in an external link, the following line in local/config.php (or a local customization file) adds title='\$LinkAlt' in $UrlLinkFmt :

$UrlLinkFmt = "<a class='urllink' href='\$LinkUrl' rel='nofollow'
  title='\$LinkAlt'>\$LinkText</a>";

Then, to set a tooltip title, use this markup:

[[http://somesite/"Tooltip title"| link text]] which will link to http://somesite/ and will have as tooltip the words in the quotes, if the user's browser is capable of it.

To add a small graphic on the right of external links like the Wikipedia fashion :
In your CSS style sheet (e.g, pub/css/local.css), add the following:

#wikibody a.external {
   background: url(external.png) center right no-repeat;
   padding-right: 13px;
   color: #2f6fab;
}
#wikibody a.external:visited {color: #006699;}

Simply put the small external.png image, which is 13px wide, in the folder where your CSS style sheet is. The one used on Wikipedia is available from http://en.wikipedia.org/skins/monobook/external.png:

Now change the above $UrlLinkFmt in your local/config.php to:

$UrlLinkFmt = "<a class='external' target='_blank' href='\$LinkUrl'>\$LinkText</a>";

To have this apply only to http: links, set the value of $IMapLinkFmt instead:

$IMapLinkFmt['http:'] = 
  "<a class='external' target='_blank' href='\$LinkUrl'>\$LinkText</a>";

Note that this adds an arrow also to Attach: and Path: links which are somehow external to the wiki but possibly internal for the website. See below for a fix.

Changing CSS styles

Q: I find Wikipedia style ext-links great, but unfortunately it transforms the attachments links into external links (check it out at MonobookSkin). Is there a way to disable it for internal links to attachments downloads? Petko

Seems like this might be complex, but it would also be nice to figure out how to take out external link images when the link itself is an image, for example, or take them out using markup for a given page or group. - JonHaupt
  • for links to the current website but outside of the wiki, you can use the Path:/path/to/page.html intermap; to style them differently than other external links, set in config.php:
    $IMapLinkFmt['Path:'] = "<a class='sitelink' href='\$LinkUrl'>\$LinkText</a>";
  • for links to the attached files, to style them differently than other external links, set in config.php:
    $IMapLinkFmt['Attach:'] = "<a class='attachlink' href='\$LinkUrl'>\$LinkText</a>";

and then, define the styles a.sitelink and a.attachlink in your CSS file. --Petko

To change the CSS-class of links to other Groups, add this to config.php:

function OtherGroupCss($group)
{
	global $pagename;
	$current = PageVar($pagename, '$Group');
	if($group == $current) return "";
	return " othergroup";
}
$FmtPV['$OtherGroupCss'] = 'OtherGroupCss($group)';
$LinkPageExistsFmt = 
   "<a class='wikilink{\$OtherGroupCss}' 
   href='\$LinkUrl'>\$LinkText</a>";
$LinkPageCreateFmt = 
   "<a class='createlinktext{\$OtherGroupCss}' rel='nofollow'
   href='{\$PageUrl}?action=edit'>\$LinkText</a>";

And then, define the style a.othergroup in your CSS file. Note: this will *add* another class to the link, together with "wikilink" or "createlinktext", you can only set the color. --Petko

Changing CSS and other tweaks

We can do this and a few other tweaks. The following modification will result in:

  • icons on external links and mailto links
  • no icon if the text of the external link is itself an image
  • suppress Attach: once a file has been uploaded, so linktext=filename

The php code is:

$LinkFunctions['http:'] = 'TxtLinkIMap';
$LinkFunctions['https:']  = 'TxtLinkIMap';
$LinkFunctions['mailto:']  = 'MailLinkIMap';
$LinkFunctions['Attach:'] = 'TxtLinkUpload';
SDV($EnableUploadOverwrite,1);
$IMapLinkFmt['Attach:'] =
    "<a class='urllink' href='\$LinkUrl' rel='nofollow'>\$UploadText</a>";
if ($EnableUploadOverwrite)
    $IMapLinkFmt['Attach:'] .=
    "<a class='createlink' href='\$LinkUpload'>&nbsp;&Delta;</a>";
$UrlLinkTxtFmt = "<span class='url'>$UrlLinkFmt</span>";
$MailLinkTxtFmt = "<span class='mailto'>$UrlLinkFmt</span>";
$UrlLinkFmt    = $UrlLinkTxtFmt;
$UrlLinkImgFmt =
    "<a class='urllinkimg' href='\$LinkUrl' rel='nofollow'>\$LinkText</a>";

function TxtLinkIMap($pagename,$imap,$path,$title,$txt,$fmt=NULL) {
  global $UrlLinkImgFmt, $UrlLinkTxtFmt;
  if (!$fmt)
      $fmt = preg_match('/^<img/',$txt) ? $UrlLinkImgFmt : $UrlLinkTxtFmt;
  return LinkIMap($pagename,$imap,$path,$title,$txt,$fmt);
}

function TxtLinkUpload($pagename, $imap, $path, $title, $txt, $fmt=NULL) {
  global $FmtV;
  $FmtV['$UploadText'] = str_replace('Attach:','',$txt);
  return LinkUpload($pagename,$imap,$path,$title,$txt,$fmt);
}

function MailLinkIMap($pagename,$imap,$path,$title,$txt,$fmt=NULL) {
  global $UrlLinkImgFmt, $MailLinkTxtFmt;
  if (!$fmt)
      $fmt = preg_match('/^<img/',$txt) ? $UrlLinkImgFmt : $MailLinkTxtFmt;
  return LinkIMap($pagename,$imap,$path,$title,$txt,$fmt);
}

Put the icon files where the css file resides and add the following to the css (some may prefer to use padding in pixels, in which case the space for the icon remains fixed if the page text is scaled):

span.url {
	background: url(external.png) top right no-repeat;
	padding-right: 0.9em;
}
span.mailto {
	background: url(mail_icon.gif) bottom right no-repeat;
	padding-right: 1.1em;
}

If you put the code into a pub/skins/skinname/skinname.php file, add a global statement to make the fmt variables accessible:

  global 
  $LinkFunctions, $UrlLinkFmt, $UrlLinkTxtFmt, $UrlLinkImgFmt, $IMapLinkFmt,
  $MailLinkTxtFmt, $EnableUploadOverwrite;

Contributed by jr on 2008-06-06.

Notes

See Also

Contributors

  • Pm - 2004-07-10, 2004-12-21

Update me

Edit - History - Print - Recent Changes - Search
Page last modified on June 05, 2008, at 07:24 PM