Extension:CustomTitle

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
CustomTitle

Release status: beta

Implementation Parser function
Description Modify the default page title and header
Author(s) Wiktor Walc (Wwalctalk)
Latest version 2008-01-30 (2008-01-30)
MediaWiki tested on 1.11.0
Database changes no
License GPL
Download see below
Example see below
Hooks used
LanguageGetMagic

Translate the CustomTitle extension if possible

Check usage and version matrix; code metrics

The CustomTitle extension allows you to modify the page header and title.

Installation[edit | edit source]

  • Create a directory CustomTitle in the extensions directory
  • Create a file in the CustomTitle directory named CustomTitle.php
  • In your LocalSettings.php add:
require_once $IP . "/extensions/CustomTitle/CustomTitle.php" ;
  • Edit the file and place the following in it:
<?php
 
# Not a valid entry point, skip unless MEDIAWIKI is defined
if (!defined('MEDIAWIKI')) {
    echo <<<HEREDOC
To install this extension, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/CustomTitle/CustomTitle.php" );
HEREDOC;
    exit( 1 );
}
 
$wgExtensionCredits['other'][] = array(
"name" => "CustomTitle",
"author" => "Wiktor Walc",
"version" => '1.0',
"url" => "https://www.mediawiki.org/wiki/Extension:CustomTitle",
"description" => "Allows to modify the page header and title"
);
 
$oCustomTitleExtension = new CustomTitle_MediaWiki();
$oCustomTitleExtension->registerHooks();
 
$wgExtensionFunctions[] = array($oCustomTitleExtension, "setupExtensionFunction");
$wgHooks['LanguageGetMagic'][] = array($oCustomTitleExtension, "addMagicWord");
 
 
class CustomTitle_MediaWiki
{
    protected $customTitle;
    protected $customPageTitle;
 
    function setupExtensionFunction()
    {
        global $wgParser;
 
        $wgParser->setFunctionHook( 'customtitle', array($this, "substitute"));        
    }
 
    function substitute(&$parser, $param1 = '', $param2 = '')
    {
        $ret = "";
        if ($param1 !== '')
            $ret .= "xxx-CustomTitleStart-xxx". $param1 ."xxx-CustomTitleEnd-xxx";
        if ($param2 !== '')
            $ret .= "xxx-CustomPageTitleStart-xxx". $param2 ."xxx-CustomPageTitleEnd-xxx";
        return $ret;        
    }
 
    function addMagicWord(&$magicWords, $langCode)
    {
        $magicWords['customtitle'] = array( 0, 'customtitle' );
 
        return true;
    }
 
    function onSkinTemplateOutputPageBeforeExec(&$m_skinTemplate, &$m_tpl)
    {
        if (isset($this->customTitle))
            $m_tpl->set('title', $this->customTitle);
        if (isset($this->customPageTitle))
            $m_tpl->set('pagetitle', $this->customPageTitle);
 
        return true;
    }
 
    function onOutputPageBeforeHTML(&$out, &$text)
    {
        if (($found = strpos($text, 'xxx-CustomTitleStart-xxx')) !== false) {
            if (preg_match("/xxx-CustomTitleStart-xxx(.*?)xxx-CustomTitleEnd-xxx/", $text, $matches)) {
                $this->customTitle = $matches[1];
                $text = str_replace($matches[0], "", $text);
            }
        }
 
        if (($found = strpos($text, 'xxx-CustomPageTitleStart-xxx')) !== false) {
            if (preg_match("/xxx-CustomPageTitleStart-xxx(.*?)xxx-CustomPageTitleEnd-xxx/", $text, $matches)) {
                $this->customPageTitle = $matches[1];
                $text = str_replace($matches[0], "", $text);
            }
        }
 
        return true;
    }
 
    public function registerHooks() {
        global $wgHooks;
 
        $wgHooks['SkinTemplateOutputPageBeforeExec'][] = array($this, 'onSkinTemplateOutputPageBeforeExec');
        $wgHooks['OutputPageBeforeHTML'][] = array($this, 'onOutputPageBeforeHTML');
    }
}

Usage[edit | edit source]

  • To change the page header and title
   {{#customtitle:My header|My Title}}
  • To change header only:
   {{#customtitle:My header}}

Important Notes[edit | edit source]

This extension modifies template variables and nothing more. You have to remember that although different title is being displayed, you still have to link to that page using the real page name.

Example[edit | edit source]

Let's say we have a page named "Example". Even if you change the title to "My own title" with {{#customtitle:My own title}}, to create a link to that page, you still have to use [[Example]].

See Also[edit | edit source]