Extension:HTML Tags

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

Release status:Extension status stable

ImplementationTemplate:Extension#type Tag
DescriptionTemplate:Extension#description Defines a tag, <htmltag>, that allows for placing HTML tags on the page out of a set of allowed tags and attributes pre-specified by the administrator.
Author(s)Template:Extension#username Yaron Koren <yaron57@gmail.com>
Latest versionTemplate:Extension#version 0.2 (May 2014)
MediaWikiTemplate:Extension#mediawiki 1.17.* or greater
Database changesTemplate:Extension#needs-updatephp No
LicenseTemplate:Extension#license GNU General Public License 2.0 or later
ExampleTemplate:Extension#example A demonstration of the HTML Tags extension
  • $wgHTMLTagsAttributes

  • <htmltag>
Hooks usedTemplate:Extension#hook

Translate the HTML Tags extension if it is available at translatewiki.net

Check usage and version matrix.

The HTML Tags extension defines a tag, <htmltag>, that can be used to display HTML tags within wiki pages that may otherwise be disallowed by the MediaWiki parser.

HTML Tags was initially developed in order to allow support the use of LRMI (Learning Resource Metadata Initiative) tags within MediaWiki pages. See Implementing LRMI for how to implement such a thing using HTML Tags.

Another potential usage of HTML Tags is to embed microdata (especially the kind that can't be embedded already using $wgAllowMicrodataAttributes).

You can see a basic demonstration of this extension here.

Code and download[edit]

You can download the HTML Tags code, in .zip format, here.

You can also download the code directly via Git from the MediaWiki source code repository. From a command line, you can call the following:

git clone https://gerrit.wikimedia.org/r/p/mediawiki/extensions/HTMLTags.git

To view the code online, including version history for each file, go here.


Warning Warning: Be very careful how you configure this extension as allowing unsafe tags will lead to an XSS vulnerability. For example, allowing <a> as suggested below could result in XSS by using code like <htmltag tagname="a" href="javascript:alert('XSS');">click here</htmltag>

After you've obtained an 'HTMLTags' directory (either by extracting a compressed file or downloading via SVN), place this directory within the main MediaWiki 'extensions' directory. Then, in the file 'LocalSettings.php' in the main MediaWiki directory, add the following line:

require_once "$IP/extensions/HTMLTags/HTMLTags.php";

You then need to specify the allowed set of HTML tags, and the allowed set of attributes for each one, using the global variable $wgHTMLTagsAttributes. For instance, to allow the <a> tag, and to allow only the attributes "href" and "class" for it, you would add the following to LocalSettings.php, after the inclusion of LocalSettings.php:

$wgHTMLTagsAttributes['a'] = array( 'href', 'class' );

If you wanted to allow the tag <fieldset>, but without any allowed attributes, you would add the following:

$wgHTMLTagsAttributes['fieldset'] = array();


Once a set of allowed tags and attributes are specified, you can use <htmltag> to place those tags on the page. You use the attribute "tagname" to specify the actual tag, and then add the other attributes exactly as you want them to appear. The "contents" of the tag become the contents of <htmltag>.

For instance, if the above lines are added, and you want to add a link on some page using the <a> tag, you could add something to the page like:

<htmltag tagname="a" href="http://en.wikipedia.org/wiki/Antarctica" class="my-links">Read about Antarctica</htmltag>

This will add the following to the page's HTML source:

<a href="http://en.wikipedia.org/wiki/Antarctica" class="my-links">Read about Antarctica</a>

Calling <htmltag> with a tag name that is not allowed will result in an error message; calling it with an attribute that is not allowed will simply lead to the attribute being ignored.

See Implementing LRMI for a full description of how this extension, along with some templates, can be used to allow for easy adding of LRMI metadata to pages.


HTML Tags was written by Yaron Koren, reachable at yaron57 -at- gmail.com.

This extension was funded by Creative Commons as part of the LRMI project.

Version history[edit]

HTML Tags is currently at version 0.2.

The version history is:

  • 0.1 - November 1, 2012 - Initial version
  • 0.2 - May 27, 2014 - i18n messages moved into JSON files; various fixes