In MediaWiki skin definitions, the BaseTemplate
that gets extended has several attributes for creating links to other pages in the wiki, but I've got a situation where I need the path to the skin's directory, to pull some images used to create the UI. By default, that would just be /skins/mySkin/images/foo.png
, by default but if someone changes the $wgStylePath
variable, or renames the skin, that would be an issue. Is there an existing variable that has that URL build out, hidden somewhere in the BaseTemplate
methods?

- 6,715
- 5
- 44
- 68
2 Answers
Yes, the SkinTemplate class, which contains the code to set up the template variables before executing the template, provides access to $wgStylePath
via the 'stylepath'
template variable.
When you subclass SkinTemplate to define your skin's main class, you are also expected to override the $stylename
member variable, which specifies the subdirectory under which your skin's own stylesheets and images reside. (This would usually be the same as the name of your skin in lower case, but it doesn't have to be; it's perfectly fine to have, say, two related skins using images from the same directory.) This is also made available as a template variable, surprisingly named 'stylename'
. So one way to specify an image path in your template would be something like:
<?php $this->text('stylepath') ?>/<?php $this->text('stylename') ?>/images/foo.png
Another way, (formerly) used e.g. by the Vector skin, is to use the getSkinStylePath()
method from the Skin class (which is the superclass of SkinTemplate; it's kind of messy and tangled for historical reasons, but basically you can pretty much treat them as one class split into two files).
Update: As of MediaWiki 1.36,
getSkinStylePath()
has been deprecated. The recommended alternative, according to the release notes, is to "replace usages with the direct path to the resources."
To use it, you pass in the name of the file as a parameter, and the method automatically prepends $wgStylePath
and $stylename
to it (and appends $wgStyleVersion
as a query string). Note that this is not a template method, so you have to escape and print the returned URL yourself:
<?php echo htmlspecialchars( $this->getSkin()->getSkinStylePath( 'images/foo.png' ) ) ?>
There's also a getCommonStylePath()
method which does exactly the same thing, except that it uses the string "common"
instead of $stylename
.

- 49,047
- 9
- 93
- 153
-
1@Wolf: I fixed the links; apparently the classes were moved into a subfolder. However, while doing so, I also noticed that at least one of the methods recommended in this answer has since been deprecated. I have not really kept up with MediaWiki development since I wrote this answer 7 years ago, and it's possible that this entire answer is now outdated. I'll leave it here in case some of it is still useful, but an up-to-date answer would be welcome. I've put a bounty on this question just in case it might attract one. – Ilmari Karonen Jan 08 '21 at 02:06
Apparently this is the new way:
$this->getSkin()->getConfig()->get( 'StylePath' ) . '/SkinName/images/foo.png';

- 3,008
- 3
- 33
- 43