To create a simple "Hello World" plugin in Moodle, you can follow these steps. We'll create a basic block plugin that displays "Hello World" on the Moodle dashboard.
Step 1: Set Up the Plugin Directory
1. Navigate to the Blocks Directory:
- Go to your Moodle installation directory.
- Navigate to the /blocks/ folder.
2. Create a New Folder:
- Create a new folder named helloworld.
Step 2: Create the Basic Plugin Files
1. Create the block_helloworld.php File:
- Inside the helloworld folder, create a file named block_helloworld.php.
- Add the following code:
php
<?php
class block_helloworld extends block_base {
public function init()
{
$this->title = get_string('helloworld', 'block_helloworld');
}
public function get_content() {
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass;
$this->content->text = 'Hello World!';
$this->content->footer = '';
return $this->content;
}
}
?>
2. Create the version.php File:
- In the helloworld folder, create a file named version.php.
- Add the following code:
php
<?php
defined('MOODLE_INTERNAL') || die();
$plugin->component = 'block_helloworld';
$plugin->version = 2024090400; // YYYYMMDDHH (date/time of creation).
$plugin->requires = 2022110800; // Requires Moodle version 4.1 or later.
$plugin->maturity = MATURITY_STABLE;
$plugin->release = '1.0';
?>
3. Create the lang Folder and Language File:
- Inside the helloworld folder, create a folder named lang.
- Inside the lang folder, create another folder named en.
- Inside the en folder, create a file named block_helloworld.php.
- Add the following code:
php
<?php
$string['pluginname'] = 'Hello World Block';
$string['helloworld'] = 'Hello World';
?>
Step 3: Install the Plugin
1. Install the Plugin:
- Go to your Moodle site.
- Navigate to
Site administration > Notifications.
- Moodle will detect the new plugin and guide you through the installation process.
2. Add the Block to a Page:
- Go to any Moodle page where blocks are allowed (e.g., Dashboard).
- Turn editing on.
- Add the "Hello World Block" from the list of available blocks.
Step 4: View the "Hello World" Message
- Once the block is added, you should see the "Hello World!" message displayed in the block.
This is a simple example to get you started with Moodle plugin development. From here, you can explore more complex features and functionality.