Extension:DynamicJsonLD
Release status: stable |
|
|---|---|
| Implementation | Parser extension |
| Description | Allows programmable access to top-level JSON-LD data via Scribunto APIs on a per-page basis. |
| Author(s) | Wolfite (Obby Wiki) (Wlft talk ) |
| Latest version | 0.1.0 (2026年06月10日) |
| MediaWiki | 1.43+ |
| Licence | GNU General Public License 3.0 or later |
| Download | GitHub:
Note: https://github.com/obbywiki/mediawiki-extensions-DynamicJsonLD/blob/main/README.md |
DynamicJsonLD is an extension which opens up programmable usage for custom JSON-LD on a per-page basis via additional Scribunto APIs.
Installation
[edit ]- Download the extension from GitHub and place the file(s) in a directory called
DynamicJsonLDin yourextensions/folder. - Add the following code at the bottom of your LocalSettings.php file:
wfLoadExtension( 'DynamicJsonLD' );
- Yes Done – Navigate to
Special:Versionon your wiki to verify that the extension is successfully installed.
- If your wiki is using the WikiSEO extension, you may need to disable the SchemaOrg generator, which is enabled by default. You can do this via your `LocalSettings.php` file:
$wgMetadataGenerators = ['OpenGraph', 'Twitter']; // this leaves the other defaults
Usage
[edit ]Passively, DynamicJsonLD adds a small non-specific JSON-LD header to every page by default. The main focus of this extension is to provide a programmable way to edit top-level JSON-LD data from any Scribunto module in Lua. The extension surfaces two methods via `mw.ext.JsonLD`:
mw.ext.JsonLD.setMainEntity(...), a method which accepts any valid JSON-LD mainEntity parameters.mw.ext.JsonLD.addData(...), a method which can be used to add miscellaneous data to the top of the JSON-LD blob.
Please note that using malformed JSON-LD schemas or other errors may affect your page rankings. This extension allows free use to add whatever data you want to via its APIs. Be careful and ensure your data is parseable and consider checking your page in Google's Rich Results Test.
Examples
[edit ]Below are some examples that may help you use this extension. Please note that if you do not require programmatic access to JSON-LD data, an extension that statically adds it such as WikiSEO or RichResults may be a better fit for your use case.
DynamicJsonLD is currently only for Scribunto modules. You will need access to create/edit modules on your wiki in order to interface with the API via Scribunto. DynamicJsonLD is best used in modules that are already passed the majority of the page's data, such as infoboxes.
Using setMainEntity to set the main topic of an article as a VideoGame
[edit ]This example is especially helpful if you use a VideoGame template or infobox on your wiki and want to display its contents on rich results. With JSON-LD, you can virtually most rich result formats, such as passing a star rating via the `aggregateRating` option.
mw.ext.JsonLD.setMainEntity( {
["@type"] = "VideoGame",
["name"] = "My Game",
["description"] = "My game description",
["url"] = "https://example.com",
["image"] = "https://example.com/wiki/Special:FilePath/Game_Thumbnail.webp",
["publisher"] = {
["@type"] = "Organization",
["name"] = "Company",
["url"] = "https://example.com",
["logo"] = {
["@type"] = "ImageObject",
["url"] = "https://example.com/logo.png",
}
}
} )
Add an image via addData
[edit ]You can also add data directly to the top of the JSON-LD blob, allowing you to do almost anything with the data.
mw.ext.JsonLD.addData( {
["image"] = "https://example.com/wiki/Special:FilePath/My_Article_Thumbnail.webp",
} )
Note that this specific example adds an image value to the JSON-LD using Special:FilePath. On many wikis, Special: may be blocked in robots.txt, meaning if you cannot allow specifically Special:FilePath/, you may have to consider an alternative.
Feedback
[edit ]This extension was developed to meet the specific needs of the Obby Wiki, but is now a public extension. Please report issues, bugs, or feature requests on the GitHub issues tab of the repository.