Manual:Tag extensions

MediaWiki, the software that runs the various Wikimedia projects, allows developers to write their own extensions to the wiki markup. An extension defines an XML-style tag which can be used in the wiki editor like this:

some text

The text between the tags gets passed on to a PHP function you implement. This function can then return a HTML string that gets inserted into the output in place of the tags and text. Note that the return string should be HTML, not wiki markup.

How to get started
MediaWiki extensions live in the  subdirectory in the MediaWiki installation. To create your own extension, first create a new file called  in this directory containing the following template code:

 some text  # the function registered by the extension gets the text between the # tags as input and can transform it into arbitrary HTML code. # Note: The output is not interpreted as WikiText but directly #      included in the HTML output. So Wiki markup is not supported. # To activate the extension, include it from your LocalSettings.php # with: include("extensions/ExampleExt.php"); $wgExtensionFunctions[] = "wfExampleExtension"; function wfExampleExtension { global $wgParser; # register the extension with the WikiText parser # the first parameter is the name of the new tag. In this case it defines the tag ...      # the second parameter is the callback function for processing the text between the tags $wgParser->setHook( "example", "renderExample" ); } # The callback function for converting the input text to HTML output function renderExample( $input ) { $output = "&lt;pre&gt;Text passed into example extension: &lt;br /&gt;". $input. "&lt;/pre&gt;"; return $output; } ?>

Now change the tag name, the function names and of course the functionality of the callback to your liking. Note that it might be better to first install and test the example extension (see next section) and only then start customizing it.

Installing the extension
All it takes is adding a single line to the end of your  file:

include("extensions/YourExtensionName.php");

and you are ready to go. Go to the sandbox and see if it works.

Timing and extensions
If you change the code for an extension, all pages that use the extension will immediately reflect the results of new code. Technically speaking, this means your code is executed each and every time a page containing the extension is rendered.

If it seems that it is not working, you may have a caching firewall or proxy that is interfering.


 * This is false; rendered pages in the parser cache and locally-cached pages will not automatically update unless there is some other cause for them to be re-rendered. --brion 23:59, 2 Mar 2005 (UTC)