FANDOM


Magic words are powerful bits of wiki coding which seem to operate by magic. They are called "magic" because they do things with one simple word that conceals rather a lot of complicated coding.

Most editors of wikis, even those very experienced with template coding, don't really understand the underlying code that makes magic words work. Hence, they are "magic" because, as was once remarked in Battlefield, any sufficiently advanced technology is indistinguishable from magic.

It is important to note that unlike templates we create on this wiki for our use, they are not categorized anywhere, least of all at Category:Templates. In a sense, they are so fundamental to the coding of a wiki that they are hidden, much like the code which makes brackets link text, as happens when you type [[Doctor Who]].

There are three main typs of magic word: variables, behavior switches, and parser functions. String functions are sometimes considered a separate category, but they are really just a specialized parser function, and so we lump the two together on this wiki.

Although this article cannot possibly hope to explain every aspect of magic words in detail, they are absolutely essential to the coding of truly powerful templates. At the end of this article, a list of good, detailed articles is given for you to explore the subject in greater detail.

Variables Edit

Some of the simplest magic words have to do with detecting dates and other numbers. Because they return values which are not constant, this type of magic word is called a variable.

Code Notes Effect
{{CURRENTMONTH}} Gives current month09
{{CURRENTMONTHNAME}}Gives current English name for monthSeptember
{{CURRENTDAY}}Gives current day 27
{{CURRENTDAYNAME}}Gives current day of week English name Sunday
{{CURRENTYEAR}}Gives current year 2020
{{CURRENTTIME}}Gives current time, in UTC08:38
{{PAGENAME}} Returns the current page's name Magic words
{{FULLPAGENAME}} Returns the current page's full name Help:Magic words
{{NUMBEROFARTICLES}} Gives # of articles in main namespace containing a link and not a redirect 86,546

These are not variables in the sense of being "user definable". The user has no control over the content of the variable. The term variable has another wiki code meaning, particularly in templates. If one wishes to elicit a response from a user in a template, such as by requesting the input of a director's name on an infobox, one would use a variable such as {{{Director}}}. Note that in wiki syntax, a user-defined variable is indicated by three curly braces {{{variable}}}, not two.

It should also be pointed out that these are not the only variable magic words that exist. Consult the standard Fandom chart at the bottom of this page for more.

Behavior switches Edit

Another type of magic word is the behavior switch. These are indicated by the format __BEHAVIORSWITCHNAME__. Note the syntax requires a one-word name in all caps enclosed in two underscores. No other variation will work. Here are some of the most common ones:

  • __NOTOC__ (can appear anywhere in the wikitext; suppresses the table of contents)
  • __FORCETOC__ (can appear anywhere in the wikitext; makes a table of contents appear in its normal position above the first header)
  • __TOC__ (places a table of contents at the word's position)
  • __NOEDITSECTION__ (hides the section edit links beside headings)
  • __NEWSECTIONLINK__ (adds a "+" link for adding a new section on a non-"Talk" page)
  • __NONEWSECTIONLINK__ (removes the "+" link on "Talk" pages)
  • __NOGALLERY__ (on a category page, replaces thumbnails with normal links)
  • __HIDDENCAT__ (on a category page, makes it a hidden category)
  • __INDEX__ (tells search engines to index the page)
  • __NOINDEX__ (tells search engines not to index the page)
DEFAULTSORT and DISPLAYTITLE Edit

Additionally, there are two very important behavior switches that impact variables. They might be thought of as "variable behavior switches".

  • {{DISPLAYTITLE:title}} (changes the displayed form of the page title)
  • {{DEFAULTSORT:sortkey}} (sets a default category sort key)

On this wiki, you should really use {{retitle}} or {{title dab away}} instead of straight up DISPLAYTITLE. On story pages, the latter italicizes the name of the story, leaving the dab term automatically unitalicized. One good use for {{retitle}} is IPod: in order to make it properly render as having a lower-case "i" at the beginning of the word iPod.

DEFAULTSORT is a particularly important magic word, and can be found working behind the scenes at {{NameSort}} and {{TitleSort}}. The former sorts a person by their last name, and the latter applies if an article begins with an article, such as "a", "an" or "the", and truncates it from that article's sortkey. Thus, Steven Moffat should have {{NameSort}} at the bottom, and The Girl in the Fireplace should have {{TitleSort}}.

Parser functions Edit

Parser functions are magic words that get the wiki software to perform an action upon a string (that is a set of letters, numbers and/or symbols) in order to return a result. Explaining all their possible functions is well beyond this scope of this help page.

However, there are two major type of parser function, conditional and non-conditional.

Conditional function Edit

A conditional function is one that results in an action based upon some sort of if/then/else logic. Here's a list of conditional functions with a very basic explanation of each:

  • {{#expr: expression }} (evaluates the given expression; see wikipedia:Help:Calculation) While not strictly conditional, it's often nested within a conditional statement so as to answer the question, "If the result of this calculation is something, then take this action; otherwise, do this other thing."
  • {{#if: test string | value if non-empty | value if empty }} (selects one of two values based on whether the test string is empty)
  • {{#ifeq: string 1 | string 2 | value if equal | value if unequal }} (selects one of two values based on whether the test strings are equal – numerically if applicable)
  • {{#iferror: test string | value if error | value if correct }} (selects value based on whether the test string generates a parser error)
  • {{#ifexpr: expression | value if true | value if false }} (selects value based on evaluation of expression)
  • {{#ifexist: page title | value if exists | value if doesn't exist }} (selects value depending on whether a page title exists)
  • {{#switch: test | case1 = value for case 1 | ... | default }} (provides alternatives based on the value of the test string)

These are not very useful within the space of a normal article. However, they are used extensively in templates. The utility of #ifeq should be readily apparent. For example, here's a silly little example which puts together variables and conditional expressions into a single statement. If we wanted to put a code on every episode page until it found Doctor Who (1996) and put up a disambiguation note this is what we'd do:

{{#ifeq: | {{PAGENAME}} | Doctor Who (1996) | :''For other uses of the name ''Doctor Who'', please see [[Doctor Who (disambiguation) |}}
  1. #ifeq: says "look at the first two things and see if they're equal"
  2. "{{PAGENAME}}" returns the value of the current page
  3. "Doctor Who (1996)" is the value we're trying to match
  4. ":''For other uses . . . " is the event we want to happen if the first two things are equal
  5. "|}}" says "if the first two things are unequal do nothing". If we wanted the code to take a positive action on inequality, we'd type something between the pipe and the curly brackets. So, we could write: {{#ifeq:|{{PAGENAME}}|Doctor Who (1996)|:''For other uses of the name ''Doctor Who'', please see [[Doctor Who (disambiguation)|This is not Doctor Who (1996)}} But that would be rather silly.

Non-conditional Edit

Non-conditional parser functions have exactly the same syntax as conditional functions, but they don't actually depend on if/then logic to produce the result. No attempt will be made here to even list all the possible parser and string functions. But we will examine one example: #titleparts.

This function separates a pagetitle into segments based on slashes, then returns some of those segments as output.

{{#titleparts: pagename | number of segments to return | first segment to return }}

If the number of segments parameter is not specified, it defaults to "0", which returns all the segments. If the first segment parameter is not specified or is "0", it defaults to "1".

Let's imagine that we had made our talk page for An Unearthly Child a little more complicated than it actually is. Consider what would happen if we gave each individual episode a subpage of its own. This, then, is how we could use #titleparts to manipulate the subpage titles:

{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls}}Talk:An Unearthly Child/Episodes/Cave of Skulls
{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 1 }}Talk:An Unearthly Child
{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 2 }}Talk:An Unearthly Child/Episodes
{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | 2 | 2 }}Episodes/Cave of Skulls

Perhaps most usefully, we could use a negative number to actually find the name of the episode, and link to it.

{{#titleparts: Talk:An Unearthly Child/Episodes/Cave of Skulls | | -1 }} Cave of Skulls

Now, you might be thinking, "How does that save any time whatsoever? I could just type [[Cave of Skulls]] and be done with it." And yes, you could. But within a template this becomes extremely powerful. With some additional coding, you could fairly easily create something that would work on any page automatically — without you having to type it on every single story page on the wiki.

And, indeed, most of the value of parser functions is not in their use on an individual page. Rather, they are the building blocks for coding templates that automatically find, manipulate, and most importantly link to information on any page.

Detailed info on magic words Edit

If you want to explore the world of template coding with magic words such as parser functions, there are a few pages you'll want to look at.

Fandom's advice on magic words Edit

A magic word is a keyword or phrase recognized by MediaWiki software (on which Fandom is built), which triggers the software to do something special on the page.

Magicword

An example of magic word input and rendering

Instructions

It's generally best to add and edit magic words using source editing mode.

To add a magic word, simply add the magic word to the content of the page somewhere. It will look like code in the editor, but after you save or preview, it will display as intended.

Visual editing

When editing in the classic rich text editor, magic words that affect the page layout will show up as a green puzzle piece, while magic words that output text will display the text, but that text will not be editable.

In the VisualEditor on the legacy platform, magic words that affect the page layout will not show up, while magic words that output text will display the text, but that text will not be editable.

On the UCP, typing the first curly brace will bring up a modal to "Add a template", which can be slightly confusing. Once the magic word is entered, and the Add template tab is clicked, there is an error message saying that "The "Template:<magic word>" template doesn't yet exist." but using the Insert tab in the upper right corner of the modal will result in the magic word being added and it will function properly.

Common magic words

Here are some of the most commonly used magic words:

  • __NOTOC__ hides the table of contents on a page.
  • __TOC__ places the table of contents exactly where this is entered. It overrides the NOTOC switch.
  • __NEWSECTIONLINK__ replaces the "Edit" button with "Add topic", for adding new sections on a non-talk page.
  • __NONEWSECTIONLINK__ replaces the "Add topic" button with "Edit", on talk namespaces.
  • __NOWYSIWYG__ disables the classic editor on a page.
  • __HIDDENCAT__ makes a category hidden.
  • {{CURRENTDAYNAME}} outputs the current day of the week.
  • {{NUMBEROFARTICLES}} outputs the number of articles on your community.
  • {{SITENAME}} outputs the name of the community.
  • {{PAGENAME}} outputs the name of the page the word is placed on.
  • {{FULLPAGENAME}} outputs the full name of the page, i.e. with the namespace prefix, of the page it is placed on.

New magic words available on UCP

  • __EXPECTUNUSEDCATEGORY__ removes the category from Special:UnusedCategories which is helpful for tracking categories which should not necessarily appear as "unused" even when they are empty.
  • {{NAMESPACENUMBER}} displays the ID (number) of the page's namespace.

Full list of magic words

For a full list of available[1] magic words, see the magic words help page on MediaWiki.

Notes

  1. Some magic words may function differently, or not work, at Fandom. This occurs because the version of the MediaWiki software used by Fandom is not always the same version as those of either MediaWiki.org or Wikipedia.
  • {{SUBJECTPAGENAME}} and {{TALKPAGENAME}} (similarly {{SUBJECTPAGENAMEE}} and {{TALKPAGENAMEE}}) may not always act inversely. For example:
    {{TALKPAGENAME:User:Example}} → Message Wall:Example
    {{SUBJECTPAGENAME:Message Wall:Example}} → Message Wall:Example
    {{SUBJECTPAGENAME:User Talk:Example}} → User:Example

See also

Further help and feedback

Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+