MediaWiki Developer's Handbook/Adding new tags

From Wikibooks, open books for an open world
Jump to navigation Jump to search

To create a parser extension for a new tag just requires a few lines of code:

<? php
if( !defined( 'MEDIAWIKI' ) ) {
  die( "This file is part of MediaWiki and is not a valid entry point\n" );
  }

$wgExtensionFunctions[] = 'myNewTagInit';
$wgExtensionCredits['myNewTag'][]= array(
  'name'         => 'myNewTag Extension', 
  'version'      => '1.0.0', 
  'author'       => 'Your Name', 
  'url'          => 'http://www.your.com/mynewtag/', 
  'description'  => 'This extension is for demonstration only.');

function myNewTagInit() {
  global $wgParser;
  $wgParser->setHook( "myNewTag", "myNewTagParse" );
  }
function myNewTagParse($content, $params, $parser ) {
  return "My ''''fancy'''' output";
  }
?>

For testing create a subdirectory mediawikipath/extensions/mynewtag. Save the coding there as myNewTag.php. Add the following line to LocalSettings.php:

require_once "$IP/extensions/mynewtag/myNewTag.php";

Now create a new mediawiki page

<mynewtag />

and save it.

The output will look like this:
My 'fancy' output