Theme plugins
A Moodle theme allows users to customize the appearance and functionality of their Moodle site, from overall design to specific activities. Users can create their own themes or modify existing ones, leveraging CSS and JavaScript for customization. The theme architecture ensures smooth fallbacks for minimal changes, fostering flexibility and ease of use.
File structure
Theme plugins are located in the /theme
directory.
Each plugin is in a separate subdirectory and consists of a number of mandatory files and any other files the developer is going to use.
View an example directory layout for a theme_example
plugin.
Some of the important files for the Theme plugintype are described below. See the common plugin files documentation for details of other files which may be useful in your plugin.
You can customize icons in base themes:
- Without altering core code by placing them in
$CFG->dataroot/pix
and$CFG->dataroot/pix_plugins
. If a theme extends a base theme and includes its own icons, those will take precedence. - Adding custom icons to a theme by placing them in the theme's
pix_core
andpix_plugins
directories, as described in the Override images section.
Similarly, mustache templates in base themes can be overridden without impacting core code by placing them in templates/[componentname]/[templatename].mustache
.
config.php
All theme options are set within the config.php
file for the theme.
View basic theme config.php
Everything is added to $THEME
. This is the theme's configuration object, it is created by Moodle using default settings and is then updated by whatever settings are added to it.
$THEME->name
. The theme's name should simply be whatever the theme's name is, most likely whatever the theme directory is named.$THEME->sheets
. An array containing the names of the CSS stylesheets to include for this theme. Boost uses SCSS instead of CSS so it doesn't list any files here.
It is just the name of the stylesheet and does not contain the directory or the file extension. Moodle assumes that the theme's stylesheets will be located in the {theme}/style
directory of the theme and have .css as an extension.
$THEME->editorsheets
. An array containing the names of the CSS stylesheets to include for text editor content area. Boost does not list any stylesheets here so text editors will use plain text styles.$THEME->layouts
. Any of the different layout types can be mapped. For more information see the layouts section.$THEME->parents
. Defines the themes that the theme will extend. Boost has no parents, but if a theme is extending boost, it should be listed it here like:
$THEME->parents = ['boost'];
$THEME->enable_dock
. Boost does not support docking blocks.$THEME->csstreepostprocessor
. Boost uses a function to post process the CSS. This is an advanced feature and is used in boost to automatically apply vendor prefixes to CSS styles.$THEME->rendererfactory
. Almost all themes need this setting to be set totheme_overridden_renderer_factory
or the theme will not be able to customise any core renderers.$THEME->undeletableblocktypes
. This is a comma separated list of block types that cannot be deleted in this theme. If you don't define this, the admin and settings blocks will be undeletable by default. Because Boost provides alternate ways to navigate it does not require any blocks.
When you first begin writing themes, make sure you take a look at the configuration files of the other themes that get shipped with Moodle. You will get a good picture of how everything works, and what is going on in a theme, simply by reading it and taking notice of what it is including or excluding.
Have a look at the following theme options for a complete list of theme options which include lesser used specialised or advanced settings:
Complete theme options
lang/en/themename.php
Language files
File path: /lang/en/plugintype_pluginname.php
Each plugin must define a set of language strings with, at a minimum, an English translation. These are specified in the plugin's lang/en directory in a file named after the plugin.
Language strings for the plugin. Required strings:
pluginname
- name of plugin.choosereadme
- descriptive text displayed beneath the theme information dialog screenshot.configtitle
- settings text for this type of plugin.
You will usually need to add your own strings for two main purposes:
- Creating suitable form controls for users who are editing the theme settings; and
- Displaying information about the theme.
View example
version.php
Version metadata
File path: /version.php
The version.php contains metadata about the plugin.
It is used during the installation and upgrade of the plugin.
This file contains metadata used to describe the plugin, and includes information such as:
- the version number
- a list of dependencies
- the minimum Moodle version required
- maturity of the plugin
View example
Insights
Getting your theme to appear correctly in theme selector
If you follow the examples on this page to the letter, when you go to the Theme Selector
page you may be discouraged to find that your theme does not appear like the other themes do. In fact, instead of your theme's name, you will see something along the lines of [pluginname](https://docs.moodle.org/dev/pluginname)
.
To correct this, you must add the theme/THEMENAME/lang/en/theme_THEMENAME.php
file, where THEMENAME
is the name of the theme folder. Inside that file, add the string $string[]('pluginname') = 'THEMENAME';
. Make THEMENAME
the name of your theme, however you want it displayed in the Theme selector.
Also, make sure to change your config.php
file and version.php
file to reflect the correct name:
$THEME->name = 'NAME';
$plugin->component = 'THEMENAME'; // Full name of the plugin (used for diagnostics)
The screenshot
for the theme should be about 500 x 400 px
.
Required theme divs
Some parts of Moodle may rely on particular divs
, for example the div with id page-header
. Consequently all themes must include at least the divs
(with the same ids) that are present in the boost
theme.
Missing out these elements may result in unexpected behaviour within specific modules or other plugins.
Caching
When Moodle is not running in theme designer mode it will look for a cached version of the compiled CSS for the current theme to serve to the browser requesting the page. If the cached file doesn't yet exist then the CSS will be built and cached during the page request.
The cached CSS is located on disk in Moodle's local cache:
<moodledata>/localcache/theme/<global_theme_revision>/<theme_name>/css/all_<theme subrevision>.css
The cache path consists of a global theme revision (themerev
config value) and a per theme sub-revision (themesubrev
plugin config value). If either of those are incremented it will change the path to the cache file and cause a new file to be generated.
Individual theme's CSS cache can be built by using the admin CLI script:
php admin/cli/build_theme_css.php --themes boost
The script will only increment the theme sub-revision of the theme(s) being built which means existing theme cache's remain untouched.