extensions/ directory of a MediaWiki installation and are activated by calling wfLoadExtension() in LocalSettings.php.
Extension structure
Every modern extension has at minimum:extension.json
The manifest file that declares metadata, autoloading, hooks, messages, and all other registration data.
PHP classes
Hook handlers, special pages, API modules, and other logic organized under a PSR-4 namespace.
i18n messages
JSON files under
i18n/ that supply translatable strings for the extension’s UI.ResourceLoader modules
Optional JS and CSS bundles declared in
extension.json and served by MediaWiki’s resource pipeline.Creating a minimal extension
The following steps build a working extension called HelloWorld that adds a notice to every page using theBeforePageDisplay hook.
Create the extension directory
Extensions must be placed inside the
extensions/ subdirectory of your MediaWiki installation.Under POSIX systems (Linux), if your extension directory is a symlink, the parent of a symbolic path refers to the link source, not the target. Check the
MW_INSTALL_PATH environment variable if your extension is not in the default location.Write extension.json
extension.json is the manifest read by ExtensionRegistry during bootstrapping. The manifest_version and name fields are required.extensions/HelloWorld/extension.json
Write the hook handler class
Create a PHP class under
src/ that implements the hook interface. Starting in MediaWiki 1.35, each hook has an associated interface with a single on* method.extensions/HelloWorld/src/HookHandler.php
Add i18n messages
Create at minimum an English message file. The key
helloworld-desc is the extension description shown on Special:Version.extensions/HelloWorld/i18n/en.json
extensions/HelloWorld/i18n/qqq.json
Register the extension in LocalSettings.php
Add a single line to
LocalSettings.php to activate the extension. This must come after the initial MediaWiki setup.LocalSettings.php
wfLoadExtension() locates extensions/HelloWorld/extension.json, processes it through ExtensionRegistry, registers autoloaded namespaces, hooks, and messages, and queues any ServiceWiringFiles.Final file layout
After following the steps above, your extension directory looks like:Complete extension.json example
This is the fullextension.json from the HelloWorld example, annotated with the purpose of each field:
How wfLoadExtension works
wfLoadExtension( 'HelloWorld' ) is a thin wrapper around ExtensionRegistry::getInstance()->queue(). ExtensionRegistry (in includes/Registration/ExtensionRegistry.php) reads the manifest, validates it against the JSON schema, and:
- Registers all
AutoloadNamespacesandAutoloadClassesentries with the PHP autoloader. - Queues
HookHandlersandHooksfor installation intoHookContainer. - Schedules
ServiceWiringFilesto be loaded when the service container is first built. - Registers
MessagesDirswith the localisation system. - Runs the optional
callbackfunction immediately after processing.
Next steps
extension.json reference
Full reference for every field in the manifest file.
Extension hooks
How to register and implement hook handlers, including service injection.
Extension services
Adding dependency-injected services to your extension.
