Adding Configurable Settings to Your Moodle Block Plugin
Hey everyone, welcome back! Chris Richter here.
Today we’re diving into PHP development for Moodle specifically, how to add configuration options to a Moodle block plugin. While I’ll be using a block plugin as the example, the same method can be applied to any Moodle plugin.
The goal is to allow administrators to configure plugin options directly through the Moodle interface, without touching any code. Let’s break down the whole process step-by-step!
Why Add Configuration Settings?
Imagine you’ve developed a block plugin that displays a welcome video. Wouldn’t it be great if admins could:
- Customise the welcome message?
- Choose whether the video should be displayed?
- Swap out the YouTube video without editing PHP files?
That’s exactly what we’re building today!
How Moodle Handles Plugin Settings
In Moodle, plugin settings are typically added under:
Site administration ➔ Plugins ➔ Blocks ➔ Your Plugin Name
Settings usually consist of a label, unique identifier, description, default value, and the input field type (text, checkbox, etc.). Let’s see how you can create this in your block plugin.
Setting Up the Code
Our plugin folder looks like this:
block_ricoshaedemo.php– Main block codelang/en/block_ricoshaedemo.php– Language stringstemplates/default.mustache– HTML templatesettings.php– Where the magic happens (we’ll start here!)
1. Create the settings.php File
First, create a settings.php file in your block’s root directory.
<?php
if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configtext(
'block_ricoshaedemo/welcome',
get_string('settingswelcomelabel', 'block_ricshaedemo'),
get_string('settingswelcomedesc', 'block_ricoshaedemo'),
'Welcome',
PARAM_TEXT,
120
));
$settings->add(new admin_setting_configtext(
'block_ricoshaedemo/youtubeid',
get_string('settingsyoutubeidlabel', 'block_ricoshaedemo'),
get_string('settingsyoutubeiddesc', 'block_ricoshaedemo'),
'',
PARAM_TEXT,
60
));
$settings->add(new admin_setting_configcheckbox(
'block_ricoshaedemo/showvideo',
get_string('settingsshowvideolabel', 'block_ricoshaedemo'),
get_string('settingsshowvideodesc', 'block_ricoshaedemo'),
0
));
}
?>
What’s happening here:
- We’re checking if the full admin tree is loaded.
- We’re adding:
- A text field for the welcome message.
- A text field for the YouTube video ID.
- A checkbox to show or hide the video.
2. Add Language Strings
Create (or edit) your language file at lang/en/block_ricoshaedemo.php:
<?php
$string['settingswelcomelabel'] = 'Ricoshae Block Welcome Text';
$string['settingswelcomedesc'] = 'Text to display at the top of the block.';
$string['settingsyoutubeidlabel'] = 'YouTube Video ID';
$string['settingsyoutubeiddesc'] = 'Enter the YouTube video ID (e.g., dQw4w9WgXcQ).';
$string['settingsshowvideolabel'] = 'Show Welcome Video';
$string['settingsshowvideodesc'] = 'Whether to display the welcome video or not.';
?>
This ensures Moodle knows what to display for labels and descriptions inside the Admin Panel.
3. Update Your Block’s Main File
Inside your block_ricoshaedemo.php, grab the configuration settings:
$this->content = new stdClass();
$this->content->text = '';
$welcome = get_config('block_ricoshaedemo', 'welcome');
$youtubeid = get_config('block_ricoshaedemo', 'youtubeid');
$showvideo = get_config('block_ricoshaedemo', 'showvideo');
$renderdata = [
'welcome' => $welcome,
'youtubeid' => $youtubeid,
'showvideo' => $showvideo
];
$this->content->text = $this->render_from_template('block_ricoshaedemo/default', $renderdata);
Here, we’re preparing variables to pass into our HTML template.
4. Adjust Your Template (Handlebars / Mustache)
In templates/default.mustache, update the HTML template like this:
<div class="block-content">
<h2>{{welcome}}</h2>
{{#showvideo}}
<div class="video-container">
<iframe width="560" height="315" src="https://www.youtube.com/embed/{{youtubeid}}" frameborder="0" allowfullscreen></iframe>
</div>
{{/showvideo}}
</div>
This template:
- Displays the welcome text.
- Embeds the YouTube video only if the showvideo setting is enabled.
Testing It Out
Now in Moodle Admin Panel:
- Go to Plugins ➔ Blocks ➔ Ricoshae Demo (or your plugin name).
- You’ll see:
- A field to set a welcome message.
- A field to input a YouTube video ID.
- A checkbox to show/hide the video.
Change the values, save, refresh your course page and boom, your block dynamically updates based on your settings!
Final Thoughts
Once you understand the flow; settings.php, language strings, getting config in your block code, and using those configs in your template you’ll be able to create fully customisable Moodle plugins that administrators will love.
This setup makes your plugins much more user-friendly and keeps your code cleaner by avoiding hard-coded values.
Want more Moodle dev tips?
Drop a comment if you’d like a deeper dive into building full-featured Moodle plugins from scratch!
Until next time – happy coding!
