Extension:LinkTarget

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
LinkTarget

Release status:Extension status beta

DescriptionTemplate:Extension#description Adds a target attribute to specified links
Author(s)Template:Extension#username Ike Hecht (Tosfostalk)
Latest versionTemplate:Extension#version 0.1.0 (April 2015)
MediaWikiTemplate:Extension#mediawiki 1.24+, probably earlier versions too
PHPTemplate:Extension#php 5.3
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license GNU General Public License 2.0 or later
Download
ParametersTemplate:Extension#parameters

$wgLinkTargetParentClasses, $wgLinkTargetDefault

Hooks usedTemplate:Extension#hook
OutputPageParserOutputManual:Hooks/OutputPageParserOutput

Translate the LinkTarget extension if it is available at translatewiki.net

Check usage and version matrix.

The LinkTarget extension allows admins to add a target attribute to internal and external links. That can be used to, for example, open internal and external links in new windows.

Installation[edit]

  • Download and place the file(s) in a directory called LinkTarget in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/LinkTarget/LinkTarget.php";
    $wgLinkTargetParentClasses = ' /*ENTER SOME CLASSES HERE*/ ';
    
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Configuration parameters[edit]

$wgLinkTargetParentClasses
The descendants of elements of this class will have their target attribute set by the extension. Expects an array with the format:
array(
     target => array( classes ),
     target => array( classes ),
      ... 
)
If target is not specified or is invalid, the target will be set to $wgLinkTargetDefault. Note that it is also valid to specify one class as a string, instead of an array of classes.
$wgLinkTargetDefault
The default target for parent classes that do not have a valid target specified.
Can be set to: _blank, _self, _parent or _top. Defaults to _blank.

Examples[edit]

Example 1: Open specific internal/external links in new windows[edit]

In LocalSettings.php:

$wgLinkTargetParentClasses = array( 'foo', 'bar' );

Adds target="_blank" as an attribute to links that are children of an HTML element that belong to either class "foo" or "bar". To use in the wiki:

This is some text with an <span class="foo">[[Internal link]]</span> and also an <span class="bar">[https://www.mediawiki.org External link]</span>.

In that example, both links will open in new windows or tabs.

Example 2: Open specific images in new windows[edit]

Using the same LocalSettings as in Example 1, in the wiki use:

<div class="bar">[[File:SomeImage|link=mediawiki.org]]</div>

Example 3: Open most links in a new window, but not these[edit]

$wgExternalLinkTarget = '_blank';
$wgLinkTargetParentClasses = array( 'foo', 'bar' );
$wgLinkTargetDefault = '_self';

In the wiki:

[[This link]] will open in a new window, but not <span class="foo">[[this one]]</span>.

Example 4: Use with extensions[edit]

Surround extension parser functions or tags with some code and open any links generated by those extensions in a new window. As an example, we'll use the Flickr API extension, with the LocalSettings code from Example 1:

<div class="foo"><flickr>SOMEIMAGEID</flickr></div>

Actually, that extension helpfully already surrounds any code that it generates with a class so really it is sufficient to set LocalSettings as follows:

$wgLinkTargetParentClasses = array( 'flickrapi' );

This will apply to all Flickr images generated by the extension. When users click on any image, the link will open in a new window or tab.

Advanced[edit]

$wgLinkTargetParentClasses = array(
     '_top' => array( 'foo'),
     array( 'bar' ),
     '_parent' => 'baz'
);

Descendants of 'foo' will have target '_top', descendants of 'bar' will have target '_blank' and descendants of 'baz' will have target '_parent'.

Known limitations[edit]

  • The links will not have any special target while viewing the page in "preview" mode.

See also[edit]