Adding a Local Plugin to Moodle’s Navigation: A Quick Guide for PHP Developers
Hello again! Chris Richter here from Ricoshae.com.au, welcome back!
Today’s post is specifically for the PHP developers out there who are working with Moodle LMS and developing custom plugins. If that’s you, keep reading. If you’re new to PHP or Moodle development, you’re still welcome to follow along you might pick up something useful!
Recently, I found myself needing to make local plugins easier to access within Moodle, without forcing users to type in URLs manually. I thought I’d share a simple, clever trick I use for adding a plugin directly into Moodle’s left-hand navigation menu, particularly when working with the Classic theme (or any theme that uses Classic’s structure).
Let’s dive into it.
The Scenario: A Simple Local Plugin
Imagine you have a local plugin called ricoshae_demo. For now, this plugin just displays a simple page with some placeholder content nothing fancy.
When you’re developing plugins, it’s common to restrict access so only users with certain permissions can view the plugin. But the real challenge is how to make the plugin easy to find, without sending users a direct link.
Instead, wouldn’t it be great if the plugin just appeared automatically in Moodle’s left-hand navigation menu?
Here’s how you can do exactly that.
Step 1: Set Up Capability-Based Access
Inside your plugin’s index.php, you should already be doing some permission checks to make sure only authorized users can access the page.
For example:
require_capability('local/ricoshae_demo:admin', context_system::instance());
This ensures that only users with the local/ricoshae_demo:admin capability can see the page.
If you’re wondering where this capability comes from, it’s defined in your plugin’s db/access.php file.
Step 2: Extend the Navigation in lib.php
Here’s the magic part: You can add your plugin to the Moodle navigation by extending the global navigation in your plugin’s lib.php file.
You’ll need to create a function like this:
function local_ricoshae_demo_extend_navigation(global_navigation $navigation) {
if (has_capability('local/ricoshae_demo:admin', context_system::instance())) {
$url = new moodle_url('/local/ricoshae_demo/index.php');
$navigation->add(get_string('pluginname', 'local_ricoshae_demo'), $url);
}
}
Let’s break it down:
- We check if the user has the required capability.
- If they do, we create a new URL pointing to our plugin’s main page (
index.php). - Then, we add a new node to the navigation menu with the plugin’s name (coming from your language string file, typically
lang/en/local_ricoshae_demo.php).
Tip: If you want the link to go somewhere other than
index.php, you can modify the$urlaccordingly.
Step 3: Language Strings
Make sure you have a language string set up for your plugin name. In your lang/en/local_ricoshae_demo.php file, you should have something like:
$string['pluginname'] = 'Ricoshae Demo';
This ensures that the menu displays a nice, user-friendly label instead of something like “local_ricoshae_demo”.
Step 4: Test It Out
After you’ve made these changes:
- Save your files.
- Go back to Moodle and refresh the page.
- If everything is set up correctly, you’ll see your plugin (
Ricoshae Demo) listed in the left-hand navigation.
Clicking on it will take users directly to your plugin’s main page!
Final Thoughts
That’s it! Just a few simple steps to make your local Moodle plugin easily accessible from the main navigation menu.
If you want more detailed walkthroughs, I’ve got some online courses where I dive deeper into:
- Creating Moodle plugins
- Building child themes
- Developing custom blocks (which is quite different from local plugins!)
You’ll find links to these courses in the description below, they might just save you a ton of time on your next Moodle project.
Got questions? Need help with your plugin? Drop a comment, I’d love to hear from you and help where I can.
Until next time, happy coding!
