Moodle Hooks Api logo

Moodle Hooks Api

Community
SaadRahman01
moodle-hooks-api

Use when implementing or migrating to the Moodle 4.4+ Hooks API. Covers hook class authoring (db/hooks.php), callback registration, dispatching, replacing legacy magic callbacks (extend_navigation, before_http_headers, etc.), and testing hook listeners.

Overview

PublisherSaadRahman01
Repositoryclaude-moodle-dev
Skill namemoodle-hooks-api
Stars
42
Forks
7
Bundled files
Instructions only
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by SaadRahman01 on GitHub. Read the source before you install it.

Installation

Install the Moodle Hooks Api AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/SaadRahman01/claude-moodle-dev/tree/main/skills/moodle-hooks-api
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/SaadRahman01/claude-moodle-dev.git /tmp/claude-moodle-dev
mkdir -p .claude/skills
cp -r /tmp/claude-moodle-dev/skills/moodle-hooks-api .claude/skills/moodle-hooks-api
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Moodle Hooks Api in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Moodle Hooks Api on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Moodle Hooks Api is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Moodle Hooks API

Overview

Moodle 4.4 introduced a typed Hooks API (core\hook\manager) replacing the unmaintainable jungle of magic callback functions like <plugin>_extend_navigation, <plugin>_before_http_headers, <plugin>_extend_settings_navigation, etc. Hooks are real classes with typed payloads, dispatched through \core\di::get(\core\hook\manager::class). Plugins register interest via db/hooks.php.

When to Use

  • Adding cross-cutting behavior triggered by core (navigation, page output, user login, course events) without monkey-patching
  • Migrating a plugin off legacy magic callbacks (deprecated 4.4+, will be removed)
  • Authoring a hook class in core or in a plugin that other plugins can listen to
  • Writing tests for hook listeners

Skip when: the event you care about is a \core\event\* (Events 2 API — different system, used for audit/logging). Hooks are for modifying behavior; Events are for reacting to facts.

Core concepts

ConceptWhere it livesPurpose
Hook classclasses/hook/<name>.phpTyped payload, optional setters for listeners to mutate
Listener registrationdb/hooks.phpMaps hook class -> callback (Class::method) + priority
Dispatcher callCore or plugin code\core\di::get(\core\hook\manager::class)->dispatch(new \plugin\hook\thing(...));
Listener methodAny classStatic or instance method taking the hook instance

Listening to a core hook

db/hooks.php:

php
<?php
defined('MOODLE_INTERNAL') || die();

$callbacks = [
    [
        'hook'     => \core\hook\output\before_standard_top_of_body_html_generation::class,
        'callback' => \local_example\hook_listener::class . '::inject_banner',
        'priority' => 100, // higher runs first
    ],
];

classes/hook_listener.php:

php
<?php
namespace local_example;

use core\hook\output\before_standard_top_of_body_html_generation as hook;

class hook_listener {
    public static function inject_banner(hook $hook): void {
        global $USER;
        if (isguestuser() || !isloggedin()) {
            return;
        }
        $hook->add_html('<div class="alert alert-info">Hello, ' . s($USER->firstname) . '</div>');
    }
}

After adding or changing db/hooks.php, purge caches: php admin/cli/purge_caches.php.

Authoring your own hook

classes/hook/before_widget_render.php:

php
<?php
namespace local_example\hook;

use core\hook\described_hook_interface;
use core\hook\stoppable_event_interface;

class before_widget_render implements described_hook_interface, stoppable_event_interface {
    private bool $stopped = false;
    private string $html = '';

    public function __construct(public readonly int $widgetid, public readonly \context $context) {}

    public static function get_hook_description(): string {
        return 'Dispatched before a widget renders. Listeners may append HTML or veto rendering.';
    }

    public static function get_hook_tags(): array {
        return ['output', 'widget'];
    }

    public function add_html(string $html): void { $this->html .= $html; }
    public function get_html(): string { return $this->html; }

    public function stop(): void { $this->stopped = true; }
    public function isPropagationStopped(): bool { return $this->stopped; }
}

Dispatch:

php
$hook = new \local_example\hook\before_widget_render($widgetid, $context);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
if ($hook->isPropagationStopped()) {
    return ''; // veto
}
echo $hook->get_html();

Migrating magic callbacks

Legacy callbackReplacement hook
<plugin>_extend_navigation\core\hook\navigation\primary_extend (4.5+) — check core for current name
<plugin>_before_http_headers\core\hook\output\before_http_headers
<plugin>_before_standard_top_of_body_html\core\hook\output\before_standard_top_of_body_html_generation
<plugin>_before_footer\core\hook\output\before_footer_html_generation
<plugin>_after_config\core\hook\after_config
<plugin>_extend_settings_navigationcheck \core\hook\navigation\* for current name

Migration steps:

  1. Search for legacy callbacks: grep -rn "function.*_extend_navigation\|_before_http_headers\|_after_config" .
  2. For each, find the matching hook class in lib/classes/hook/ of your Moodle install.
  3. Create db/hooks.php mapping; move the callback body into a listener class.
  4. Delete the legacy function from lib.php.
  5. Bump version.php, purge caches, run tests.

Testing hook listeners

php
<?php
namespace local_example;

defined('MOODLE_INTERNAL') || die();

final class hook_listener_test extends \advanced_testcase {
    /** @covers \local_example\hook_listener::inject_banner */
    public function test_banner_injected_for_logged_in_user(): void {
        $this->resetAfterTest();
        $this->setUser($this->getDataGenerator()->create_user());

        $hook = new \core\hook\output\before_standard_top_of_body_html_generation();
        \core\di::get(\core\hook\manager::class)->dispatch($hook);

        $this->assertStringContainsString('Hello,', $hook->get_output());
    }

    public function test_skipped_for_guest(): void {
        $this->resetAfterTest();
        $this->setGuestUser();

        $hook = new \core\hook\output\before_standard_top_of_body_html_generation();
        \core\di::get(\core\hook\manager::class)->dispatch($hook);

        $this->assertStringNotContainsString('Hello,', $hook->get_output());
    }
}

CLI: list registered hooks

bash
php admin/cli/hooks_list.php          # all hooks + listeners
php admin/cli/hooks_list.php --hook=core\\hook\\output\\before_http_headers

Gotchas

  • Purge caches after any db/hooks.php change. Listener registration is cached.
  • Priority is a hint — order between equal priorities is undefined. Don't rely on it for correctness.
  • Stoppable hooks: only implement stoppable_event_interface if vetoing is meaningful. Most output hooks are not stoppable.
  • Don't dispatch hooks from constructors or setUp() — they can have side effects.
  • DI container: always resolve manager via \core\di::get(...). Don't new it.
  • Backporting: pre-4.4 plugins still need legacy callbacks. Either keep both with a version check, or drop pre-4.4 support and bump requires in version.php.

Checklist

  • db/hooks.php exists with correct hook class FQCN
  • Listener class is autoloadable (under classes/ with PSR-4)
  • Caches purged after edit
  • Tests cover both the action and the no-op branch
  • Legacy callback removed (or version-gated) after migration
  • version.php bumped

Frequently asked questions

What does the Moodle Hooks Api AI skill do?

Use when implementing or migrating to the Moodle 4.4+ Hooks API. Covers hook class authoring (db/hooks.php), callback registration, dispatching, replacing legacy magic callbacks (extend_navigation, before_http_headers, etc.), and testing hook listeners.

Why use Moodle Hooks Api on TypingMind?

Because you install it once and use it with any model. Moodle Hooks Api is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Moodle Hooks Api in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/SaadRahman01/claude-moodle-dev/tree/main/skills/moodle-hooks-api. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Moodle Hooks Api?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Moodle Hooks Api?

As many as you like. As long as a model supports skills, you can use Moodle Hooks Api with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Moodle Hooks Api AI skill free?

Yes. It is published on GitHub by SaadRahman01 under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇