GRAV CMS DEVELOPER

WhatsApp/Signal: +91-953-66666-77
Email ID: hello@gravdeveloper.com
Writing a Simple Grav CMS Plugin

Writing a Simple Grav CMS Plugin

  • Date: May 26, 2025
  • Tags: GRAV CMS

Grav CMS is a modern, flat-file content management system that offers a flexible plugin architecture. Plugins in Grav allow developers to extend the core functionality without modifying the core codebase. In this tutorial, we'll walk through the steps to create a simple Grav plugin using the DevTools plugin.

Prerequisites

Before we begin, ensure you have the following:

A working installation of Grav CMS.

Command-line access to your Grav project directory.

The DevTools plugin installed. Grav Documentation

Step 1: Install the DevTools Plugin

The DevTools plugin provides a command-line interface to generate scaffolding for new plugins, themes, and more.

To install DevTools via the command line:

Bash

  • bin/gpm install devtools

Alternatively, you can install it through the Admin Panel:

Navigate to the Plugins section.

Click on Add.

Search for DevTools and click Install. Grav Documentation theskylab.net

Step 2: Generate a New Plugin

With DevTools installed, you can now generate a new plugin scaffold: Grav Documentation

Bash:

  • bin/plugin devtools new-plugin

You'll be prompted to enter details about your plugin:

Plugin Name: MyPlugin

Plugin Description: A simple plugin example.

Developer Name: Your Name

Developer Email: you@example.com Wikipedia

After completing the prompts, DevTools will create a new plugin directory under user/plugins/myplugin.

Step 3: Understand the Plugin Structure Navigate to your newly created plugin directory:

Bash:

  • cd user/plugins/myplugin

You'll find the following files:

myplugin.php: The main plugin class file.

myplugin.yaml: The plugin's configuration file.

blueprints.yaml: Defines the plugin's configuration options for the Admin Panel.

languages.yaml: Contains translation strings.

Step 4: Implement Plugin Functionality

Open myplugin.php and you'll see a class extending Plugin. You can subscribe to Grav events by defining the getSubscribedEvents method. Creative Bloq

Here's an example that adds a message to the page content:

PHP

<?php namespace Grav\Plugin;

use Grav\Common\Plugin;

class MyPlugin extends Plugin { public static function getSubscribedEvents() { return [ 'onPageContentRaw' => ['onPageContentRaw', 0] ]; }

public function onPageContentRaw($event)
{
    $content = $event['page']->getRawContent();
    $content .= "\n\n**This content was added by MyPlugin!**";
    $event['page']->setRawContent($content);
}

} This plugin appends a bold message to the end of every page's raw content.

Step 5: Enable the Plugin

Ensure your plugin is enabled by checking the myplugin.yaml file:

yaml:

  • enabled: true

If you're using the Admin Panel, navigate to Plugins > MyPlugin and toggle the plugin to Enabled.

Step 6: Test Your Plugin

Visit any page on your Grav site, and you should see the message "This content was added by MyPlugin!" appended at the end.

Conclusion

Creating plugins in Grav CMS is straightforward, thanks to its event-driven architecture and tools like DevTools. By following this guide, you've learned how to set up a basic plugin that modifies page content. From here, you can explore more complex functionalities and integrate with other Grav features. Grav Documentation

For more information, refer to the official Grav documentation:

Plugin Tutorial

Plugin Basics

Feel free to reach out if you'd like to make changes to your Grav website or simply chat about your Grav project. I'm available for a free consultation!