Manual:Hooks/BeforePageDisplay

From Linux Web Expert

BeforePageDisplay
<translate> Available from <tvar name=1><translate> version <tvar (r14420, codereview)</tvar></translate>
Allows last minute changes to the output page, e.g. adding of CSS or JavaScript by extensions.
<translate> Define function:</translate>
public static function onBeforePageDisplay( MediaWiki\Output\OutputPage $out, Skin $skin ) { ... }
<translate> Attach hook:</translate> <translate> In <tvar name=1>extension.json</tvar>:</translate>
{
	"Hooks": {
		"BeforePageDisplay": "MediaWiki\\Extension\\MyExtension\\Hooks::onBeforePageDisplay"
	}
}
<translate> Called from:</translate> <translate> File(s):</translate> Output/OutputPage.php
<translate> Function(s):</translate> output
<translate> Interface:</translate> BeforePageDisplayHook.php

<translate> For more information about attaching hooks, see <tvar name=1>Manual:Hooks </tvar>.</translate>
<translate> For examples of extensions using this hook, see <tvar name=cat>Category:BeforePageDisplay extensions</tvar>.</translate>

Details

  • This hook was in SkinTemplate.php until version 1.12.0
  • $out - The OutputPage object.
  • $skin - Skin object that will be used to generate the page, added in 1.13.

Usage

Scripts, styles, and other header elements can be added to the OutputObject at this point.

  • $out->addMeta( $name, $value ) - Add a ‎<meta> tag.
MediaWiki version:
<translate> ≥</translate> 1.17

For adding scripts and styles on MediaWiki 1.17 and above, use ResourceLoader modules.

  • $out->addModules( [ /*modules*/ ] ) - For modules with both scripts and styles
  • $out->addModuleStyles( [ /*modules*/ ] ) - For modules with styles only
<td class="mw-version-versionbox" title="<translate nowrap> MediaWiki <tvar name=1>1.16</tvar> is unsupported version</translate>">
<translate> ≤</translate> 1.16
MediaWiki version:

For adding scripts and styles on MediaWiki 1.16 and below:

  • $out->addScriptFile( $path ) - Add a JS file. $path is the path to the JS file, such as $wgScriptPath /extensions/MyExtension/MyCoolScript.js
  • $out->addScript( $html ) - Add a JS file. $html is a full script tag: '<script type="text/javascript" src="..."></script>'
  • $out->addStyle( $src ) - Add a CSS file. $src is a URL to a stylesheet.
  • $out->addInlineScript( $script ) - Add inline JS.
  • $out->addLink( $title, $id ) - Add a link for the keyword ‎<link> tag.

See also