Extension:AllowAnchorTags
Jump to navigation
Jump to search
![]() | This extension is currently not actively maintained! Although it may still work, any bug reports or feature requests will more than likely be ignored. |
![]() | This extension stores its source code on a wiki page. Please be aware that this code may be unreviewed or maliciously altered. They may contain security holes, outdated interfaces that are no longer compatible etc. Note: No localisation updates are provided for this extension by translatewiki.net . |
AllowAnchorTags Release status: unmaintained |
|
---|---|
Implementation | Tag |
Description | Adds <anchor>...</anchor> tags and parses them to <a href=""></a> tags. |
Latest version | 1.0 |
MediaWiki | 1.9+ |
Database changes | No |
License | No license specified |
Download | See code section |
<anchor> |
|
The AllowAnchorTags extension adds <anchor>...</anchor>
tags and parses them to <a href=""></a>
tags.
Usage[edit]
The URL must be specified in the following format: <anchor url='http://someurl.com' target='_blank'>Some Text</anchor>
Installation[edit]
- Download and place the file(s) in a directory called
AllowAnchorTags
in yourextensions/
folder. - Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/AllowAnchorTags/AllowAnchorTags.php";
Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.
Code[edit]
- AllowAnchorTags.php
<?php
# Extension credits that show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
'name' => 'AllowAnchorTags',
'version' => 1.0,
'author' => ' Amolsodhi',
'url' => 'https://www.mediawiki.org/wiki/Extension:AllowAnchorTags',
'description' => 'Adds <tt><anchor></tt> tag and parses it to <tt><nowiki><a href=""></a></nowiki></tt> tags',
);
# Defines the main function to be executed for this extension.
$wgExtensionFunctions[] = 'addAnchorTag';
# Sets the hook to be executed once the parser has stripped HTML tags.
$wgHooks['ParserAfterStrip'][] = 'addAnchorTag';
# This function initiates the hook for the parser to convert <anchor></anchor>
# tags to <a href=''></a> tags.
function addAnchorTag() {
// Declaring the global parser..
global $wgParser;
// Setting the hook to parse <anchor></anchor> tags from the parser output..
$wgParser->setHook( 'anchor', 'startAddAnchor' );
// Extensions required to return true
return true;
}
# This function extracts the parameters from the <anchor></anchor> tags and
# the text between the <anchor> and </anchor> tags and formats them as "<a href=''>"
# tags and writes them in the document.
function startAddAnchor( $input, $argv ) {
// Matching to see if the URL matches the prefixes in $wgUrlProtocols..
if (preg_match("/^(http:\/\/|https:\/\/|ftp:\/\/|irc:\/\/|gopher:\/\/|news:|mailto:)/", $argv['url'])) {
// Fetching the 'url' parameter..
$url = $argv['url'];
} else {
$url = '';
}
// Fetching the 'target' parameter..
if(isset($argv['target'])) {$target = $argv['target'];} else { $target = '';}
if ($url != '' && $target != '') {
// If a target parameter exists then print the link with it..
return "<a href=\"" . htmlspecialchars($url) . "\" target=\"" . htmlspecialchars($target) . "\">" . htmlspecialchars($input) . "</a>";
} else if ($url != '') {
// If a target parameter does not exist then just print the link with the 'href' URL..
return "<a href=\"" . htmlspecialchars($url) . "\">" . htmlspecialchars($input) . "</a>";
} else {
return '';
}
}
Modification to allow the use of <IMG...>[edit]
Replace
if ($url != '' && $target != '') {
// If a target parameter exists then print the link with it..
return "<a href=\"" . htmlspecialchars($url) . "\" target=\"" . htmlspecialchars($target) . "\">" . htmlspecialchars($input) . "</a>";
} else if ($url != '') {
// If a target parameter does not exist then just print the link with the 'href' URL..
return "<a href=\"" . htmlspecialchars($url) . "\">" . htmlspecialchars($input) . "</a>";
with
if (strncasecmp($input,'<img ',5) == 0) {
$body = "<" . htmlspecialchars(preg_replace("/(^<)(.+)(>$)/","$2",$input)) . ">";
} else {
$body = htmlspecialchars($input);
}
if ($url != '' && $target != '') {
// If a target parameter exists then print the link with it..
return "<a href=\"" . htmlspecialchars($url) . "\" target=\"" . htmlspecialchars($target) . "\">" . $body . "</a>";
} else if ($url != '') {
// If a target parameter does not exist then just print the link with the 'href' URL..
return "<a href=\"" . htmlspecialchars($url) . "\">" . $body . "</a>";
When using <IMG...> in this way, you must use single quotes - for example:
<anchor url='http://www.site.tld'><img align='left' src='url'></anchor>
See also[edit]
- Extension:AnchorHandler – Allows inserting <a> anchor tags into wikitext
- Extension:HTML Tags – Adds
<htmltag></htmltag>
tags, that can be used to display HTML tags within wiki pages that may otherwise be disallowed by the MediaWiki parser.