Manual:Variable/en


 * This page is about creating variables. For help using default variables, see . For PHP global variables used internally in MediaWiki, see Manual:wg variable.

Variables are bits of wikitext that look like templates but have no parameters and have been assigned hard-coded values. Standard wiki markup such as or  are examples of variables. You can also extend wiki markup by defining your own custom variables.

The term is something of a misnomer because there is nothing variable about a variable. End users cannot change the value of a variable because it is predetermined by a bundle of PHP code that calculates its value. The term "variables" comes from the source of their value: a PHP variable or something that could be assigned to a variable, e.g. a string, a number, an expression, or a function return value.

Defining custom variables
Variables are a special case of magic words so our first step will be to define the variable as a magic word:


 * 1) Choose a magic word ID for your variable. This is only an internal identifier that is used to tie together the various parts of the variable definition: the names that appear in wiki text, and the PHP code that assigns a value to the variable. It is best to choose an ID that will be uniquely associated with your extension and is unlikely to be confused with other magic word IDs used by other extensions. A common strategy is to use something like  where canonicalname is the name under which you will register your extension (see Registering custom variables below).
 * 2) Define the names that will appear in wiki text. To accomplish this, you will need to define a  array in a file. See  for more information. Your names can be case sensitive and language dependent.
 * 3) Provide PHP code to assign a value to the variable. To accomplish this, you will need to define and register a hook function with.

Note that the only difference between this process and the general process for defining magic words is the last step: defining a hook function for. Parser functions have a different method for associating an ID with PHP code. See Manual:Parser functions for more information.

Registering custom variables
This is a two step process:


 * 1) Define the variable so that it gets included in . This requires adding a member to  .  For more information, please see Registering features with MediaWiki.
 * 2) Declare the magic word ID as a variable. To accomplish this we write and assign a hook to, the subject of this article.

Example
''Note: if you wish to use this example as a coding template, please replace  with something unique to your project so that there is less risk of name clashes with MediaWiki or any of its extensions. For example, if your extension was named  and you were reasonably sure that no-one else had an extension with constants, variables, functions, or classes beginning 'BEST_', 'wgBEST', 'wfBEST', or even 'BEST' you might want to replace 'My' with 'BEST'.''

File

File

For more information

 * - discusses default magic words built into the core MediaWiki package
 * - reviews the different kinds of magic words and how MediaWiki tells apart variables, parser functions, and templates.
 * Markup spec