Creating a plugin
A plugin adds widgets to the builder, reacts to events, modifies the HTML of pages, or declares its own pages and routes. GeniusPress core widgets use exactly the same API as plugins.
License. Thanks to the exception in
LICENSE-EXCEPTION.md, a plugin that only uses the public API described here can have the license of your choice, including a commercial one.
Get started in 2 minutes
php artisan geniuspress:make-plugin temoignage --name="Testimonial" --author="My NGO"
The command creates plugins/temoignage/ with a working widget. Activate it in Admin → Plugins, then open the builder: the "Testimonial" widget is in the list of elements.
Structure
plugins/temoignage/
├── plugin.json Identity, compatibility, loaded files
├── src/Plugin.php Entry point: declares widgets and hooks
├── src/Widget.php Allowed settings and HTML rendering on the site
├── public/builder.js Controls and preview in the builder
├── public/site.css Styles on the site (and in the builder canvas)
├── database/migrations/ (optional) plugin tables, migrated on activation
├── resources/views/ (optional) Blade views, available via $this->views()
└── README.md
The folder name is the plugin identifier: lowercase letters, digits and hyphens.
plugin.json
{
"name": "Testimonial",
"version": "1.0.0",
"description": "Adds a testimonial widget.",
"author": "My NGO",
"license": "MIT",
"homepage": "https://exemple.org",
"requires": { "geniuspress": ">=0.1.0" },
"main": "Plugins\\Temoignage\\Plugin",
"autoload": { "psr-4": { "Plugins\\Temoignage\\": "src/" } },
"assets": {
"builder": ["builder.js"],
"site_css": ["site.css"],
"site_js": []
}
}
| Field | Purpose |
|---|---|
name, version |
Required. |
requires.geniuspress |
Minimum version: >=0.1.0, ^1.0 or *. An incompatible plugin cannot be activated. |
main |
Class that extends App\Plugins\Plugin. Optional for a plugin that only provides files. |
autoload.psr-4 |
Namespace → folder, as in Composer. |
assets |
Files from the public/ folder: builder (builder scripts), site_css and site_js (public site). |
The plugin class
namespace Plugins\Temoignage;
use App\Plugins\Plugin as BasePlugin;
use Illuminate\Support\Facades\Route;
class Plugin extends BasePlugin
{
public function boot(): void
{
$this->widget(new Widget);
$this->on('page.published', fn ($page) => logger("Published: {$page->title}"));
$this->filter('page.head', fn (string $html) => $html.'<meta name="theme-color" content="#0f172a">');
$this->views(); // views available via view('temoignage::name')
$this->routes(function () {
Route::get('/merci', fn () => view('temoignage::merci'))->name('merci');
});
}
public function activate(): void {} // once, on activation (after migrations)
public function deactivate(): void {} // once, on deactivation: do not delete data
}
| Method | Effect |
|---|---|
$this->widget(Widget) |
Declares a widget. |
$this->on(event, callback) |
Reacts to an event. |
$this->filter(name, callback) |
Modifies a value; the callback receives the value and returns it. |
$this->routes(callback) |
Routes prefixed with /plugins/{folder}, with session and CSRF protection. Add ->middleware('auth') for the admin. |
$this->views() |
Views from resources/views under the {folder}:: namespace. |
$this->path('path'), $this->asset('file') |
Absolute path of a plugin file, public URL of a file in public/. |
Creating a widget
Server side
namespace Plugins\Temoignage;
use App\Builder\RenderContext;
use App\Builder\Widgets\Widget as BaseWidget;
class Widget extends BaseWidget
{
public function type(): string { return 'temoignage'; } // unique, lowercase and hyphens
public function label(): string { return 'Testimonial'; }
public function rules(): array
{
return [
'text' => 'string:600',
'author' => 'string:120',
'photo' => 'url',
'accent' => 'color',
'align' => 'enum:left,center',
];
}
public function render(array $s, string $id, RenderContext $ctx): string
{
$ctx->rule($id, ['border-color' => $s['accent'] ?? null], false, ' blockquote');
return '<blockquote><p>'.e($s['text'] ?? '').'</p></blockquote>';
}
}
Validation rules. Any setting missing from rules() is removed before saving, and every value is checked:
| Rule | Accepts |
|---|---|
string:MAX |
Plain text, truncated to MAX characters. Escape it with e() in render(). |
html |
HTML cleaned with an allowlist (paragraphs, bold, italic, lists, links). |
color |
#rgb, #rrggbb, #rrggbbaa, rgb()/rgba(), transparent, or a global site color (global:primary, global:{id}). |
int:MIN:MAX |
Integer, clamped between MIN and MAX. |
enum:a,b,c |
One of the listed values. |
url |
https://, http://, mailto:, tel:, /path or #anchor. Never javascript:. |
slug:MAX |
Lowercase identifier (anchors). |
bool |
True or false. |
dim |
CSS dimension: 50%, 320px, 1.5em, 1.4 (unitless) or auto. |
spacing |
Four sides { t, r, b, l, u } (margins, radii). |
shadow |
Shadow { x, y, blur, spread, color, inset }. |
font |
A font offered by GeniusPress, or global:heading / global:body. |
Add @r to a rule for a per-device setting ('font_size' => 'int:8:120@r' also accepts font_size_tablet and font_size_mobile), @h for a hover setting ('color' => 'color@h' also accepts color_hover), or @rh for both. Add + Schema::TYPOGRAPHY to your rules to offer full typography settings.
The style settings shared by all elements (margins, width, background, gradient, border, shadow, opacity, per-device visibility, anchor, CSS classes…) are added automatically and applied to the widget wrapper. A setting of your widget with the same name replaces them.
RenderContext
$ctx->rule($id, [property => value], $device = false, $suffix = '')adds a CSS rule for#gp-{id}{suffix}. Null values are ignored.$deviceisfalse(all screens),'tablet'(1024 px and below) or'mobile'/true(767 px and below). For hover, use a suffix such as' blockquote:hover'.$ctx->responsive($id, $settings, 'font_size', fn ($v) => ['font-size' => $v.'px'], ' blockquote')writes the rule for every device where the setting is defined.$ctx->typography($id, $settings, ' blockquote')applies theSchema::TYPOGRAPHYsettings and loads the chosen font.- Global colors (
global:primary) are replaced with their CSS variable automatically. RenderContext::px($n)returns"{n}px"ornull.$ctx->media($id)returns the media library image matching amedia_idsetting.
Optional methods
translatable(): settings translated by the AI assistant (['text', 'author']).aiHint(): one sentence, in English, that tells the AI assistant when to use the widget. Without it, the AI does not use the widget to generate pages.aiExcluded(): settings the AI must not fill in (image URLs, links).
Builder side (public/builder.js)
No build tooling is needed: GeniusPress provides React through window.GeniusPress.
const { registerWidget } = window.GeniusPress;
registerWidget('temoignage', {
label: 'Testimonial',
icon: 'M7 7h4v6H5M17 7h4v6h-6', // 24×24 SVG path
defaults: { text: 'Your testimonial', align: 'left' },
controls: [
{ key: 'text', label: 'Text', type: 'textarea' },
{ key: 'author', label: 'Author', type: 'text' },
{ key: 'photo', label: 'Photo', type: 'image' },
{ key: 'accent', label: 'Color', type: 'color', tab: 'style' },
{ key: 'align', label: 'Alignment', type: 'segmented', tab: 'style',
options: [{ value: 'left', label: 'Left' }, { value: 'center', label: 'Center' }] },
],
preview: (o, { h }) => h('blockquote', null, h('p', null, o.text)),
css: (o, { rule, tablet, mobile, responsive, typography, px, sel }) => rule(`${sel} blockquote`, { 'border-color': o.accent }),
});
Control types: text, textarea, richtext, url, select, segmented, slider (min, max, step, default), color, toggle, image, links, dim (units), spacing (units), shadow, font, and heading (group title). Choose the tab with tab: 'content' | 'style' | 'advanced'. Add responsive: true for a per-device setting (@r rule) and hover: true for a hover setting (@h rule).
In css, rule, tablet and mobile take a full selector; responsive(key, fn, suffix) and typography(suffix) do the same as their PHP counterparts.
preview must produce the same HTML as render() in PHP, and css the same rules. If preview throws an error, only that widget shows "Preview unavailable": the builder stays usable.
A plugin without builder.js works too. GeniusPress then generates the controls from the PHP rules, and the preview is shown on the published site.
Available hooks
| Name | Type | Receives | Use |
|---|---|---|---|
page.saved |
event | Page |
After every save in the builder. |
page.published |
event | Page |
After a publication. |
page.content |
filter | validated JSON tree ({ version: 2, elements: [...] }, nested containers) |
Modify the content just before rendering. App\Builder\Renderer::walk() visits every element. |
page.head |
filter | string $html, Page |
Add tags to <head>. |
page.body_end |
filter | string $html, Page |
Add HTML before </body>. |
admin.menu |
filter | array $nav |
Add a menu entry: [route name, label, SVG path]. |
A hook function that throws an exception is ignored and logged: it never breaks the page.
Security and robustness
- A plugin runs PHP with full privileges. Only administrators can activate one.
- A plugin that crashes on load is deactivated automatically, and the error is shown in Admin → Plugins.
- Deactivated plugin: its widgets stay in the pages but are no longer displayed. They reappear on reactivation, with no loss of content.
- Public files: only the plugin's
public/folder is served, and only for known file types (js, css, images, fonts, json). - Escape all text with
e()inrender(). Onlyhtmlsettings are already cleaned.
Complete example
The plugins/citation folder contains a complete plugin generated by make-plugin: a widget, its styles and its preview in the builder.