Home / Docs / hooks
Developer Guide

Action & Filter Hooks

App\Core\Hook API, post lifecycle events, comment events, and filter pipelines.

Action & Filter Hooks

Encrypt CMS provides an event bus and filter pipeline via App\Core\Hook.


1. Action Hooks vs. Filter Hooks

  • Actions: Execute code in response to Core events without altering data return values (e.g., logging an event, invalidating a cache, sending an email).
  • Filters: Intercept, modify, and return data before it is rendered or stored (e.g., modifying post content, altering page titles, filtering excerpt lengths).

2. Using Actions

Registering an Action Callback

use App\Core\Hook;

Hook::addAction(
    'post.published',
    function (array $context): void {
        $postId = $context['id'] ?? 0;
        // Invalidate reverse proxy cache or ping webhook
    },
    $priority = 10,
    $acceptedArgs = 1
);

Triggering an Action

Hook::doAction('my_plugin.completed', $contextData);

3. Using Filters

Registering a Filter Callback

use App\Core\Hook;

Hook::addFilter(
    'content.filter',
    function (string $content): string {
        // Append a custom disclaimer or embed
        return $content . '<p class="disclaimer">Published via Encrypt CMS.</p>';
    },
    $priority = 10,
    $acceptedArgs = 1
);

Applying a Filter

$filteredContent = Hook::applyFilters('content.filter', $rawContent);

4. Authoritative Core Hooks Reference

Hook Name Type Arguments Description
post.created Action array $postData Fired immediately after a new post record is inserted.
post.published Action array $postData Fired when post status transitions to 'published'.
post.updated Action array $context Fired when an existing post is modified.
post.trashed Action int $postId Fired when a post is moved to trash.
post.restored Action int $postId Fired when a post is restored from trash.
post.deleted Action int $postId Fired on permanent post deletion.
page.created Action array $pageData Fired when a new static page is created.
page.updated Action array $context Fired when an existing page is updated.
page.deleted Action int $pageId Fired on permanent page deletion.
comment.created Action array $commentData Fired when a comment is submitted.
comment.approved Action int $commentId Fired when a comment is approved by a moderator.
comment.updated Action array $commentData Fired when a comment is edited or updated.
comment.deleted Action int $commentId Fired on permanent comment deletion.
user.login Action array $userData Fired upon successful user authentication.
content.filter Filter string $content Filters article content before HTML rendering.
title.filter Filter string $title Filters page and post titles before rendering.

5. Hook Guarantees

  • Exception Isolation: If a third-party plugin hook throws an unhandled exception, Hook catches and logs it. Remaining callbacks in the chain continue running unaffected.
  • Priority Ordering: Callbacks execute in ascending priority order (lower numbers execute first, default 10). Equal priorities run in FIFO registration order.
  • Recursion Bounds: Hook depth is limited to 30 recursive calls to prevent runaway infinite loops.