Plugin Development

Plugins extend Cainty's functionality using a hook-based system inspired by WordPress.

Plugin Structure

plugins/my-plugin/
├── plugin.json     # Plugin metadata (required)
└── boot.php        # Entry point (required)

plugin.json

{
    "name": "My Plugin",
    "version": "1.0.0",
    "author": "Your Name",
    "description": "What this plugin does"
}

boot.php

The entry point for your plugin. This file is included when the plugin is active:

<?php
use Cainty\Plugins\Hook;

// Your plugin code here
Hook::on('header_after', function () {
    echo '<div class="notice">Hello from my plugin!</div>';
});

Actions (Hook::on)

Actions let you execute code at specific points in the page lifecycle:

// Register an action
Hook::on('event_name', function () {
    // Your code runs here
});

// Register with priority (lower = earlier, default 10)
Hook::on('event_name', function () {
    // Runs before default priority
}, 5);

Available Action Hooks

HookWhen It Fires
head_metaInside <head>, for adding meta tags or styles
header_afterAfter the site header
footer_beforeBefore the site footer

Filters (Hook::filter)

Filters let you modify data as it passes through the system:

// Modify post content before rendering
Hook::filter('content_render', function ($content) {
    // Modify and return the content
    return $content . '<p>Added by my plugin</p>';
});

Available Filters

FilterData Passed
content_renderPost/page HTML content before display

Firing Hooks

In your own code or templates, you can fire custom hooks:

// Fire an action
Hook::fire('my_custom_action');

// Apply a filter
$data = Hook::apply('my_custom_filter', $data);

Shortcodes

Plugins can register custom shortcodes:

use Cainty\Shortcodes\ShortcodeEngine;

ShortcodeEngine::register('greeting', function ($attrs) {
    $name = $attrs['name'] ?? 'World';
    return "<p>Hello, " . htmlspecialchars($name) . "!</p>";
});

Usage in content: [greeting name="Cainty"]