Skip to content

PHP API, Hooks & CLI

Htmx exposes $this->wire()->htmx (ProcessWire\Htmx) and $this->wire()->htmxCache (Totoglu\Htmx\HtmxCache), one ProcessWire hook, and three processwire-console CLI commands.

Core Service Methods ($this->wire()->htmx)

Section titled “Core Service Methods ($this->wire()->htmx)”
Method / Property Purpose
$htmx->request Typed Totoglu\Htmx\Request inspector (isHtmx(), isBoosted(), isPartial(), isQuery(), validateCsrf(), fake()).
$htmx->response Typed Totoglu\Htmx\Response builder (redirect(), refresh(), location(), pushUrl(), retarget(), reswap(), trigger(), stopPolling(), vary()).
$htmx->fragment Typed Totoglu\Htmx\Fragment renderer (addPartial(), renderPartials(), renderFile()).
$htmx->cache Active Totoglu\Htmx\HtmxCache instance (also available as $this->wire()->htmxCache).
registerComponent(string $alias, string $class) Adds a Component class to the explicit /hx/req allowlist.
renderComponent(string $aliasOrClass, array $props = []) Mounts, authorizes, and renders a stateful Component.
use(array|string $extensions) Queues one or more bundled HTMX 4 extensions for the current page.
loadExtension(string $name) Loads a single bundled HTMX 4 extension by name.
loadHyperscript() Enqueues the bundled Hyperscript runtime for the current page.
handleEndpoint() Dispatches incoming /hx/req component requests.
resolveRegisteredComponentClass(string $alias) Resolves an allowlisted component alias to its FQCN.
resolveNormalizedEndpointPath() Returns the normalized endpointUrl path used in HKDF state binding.
endpointsMatch(string $a, string $b) Compares two normalized endpoint paths safely.
isAdminContext() Returns true when running inside the ProcessWire admin.
getAssetConfigToken() Returns the deterministic cache-busting token for bundled assets.
isSafeAssetUrl(string $url) Validates that a script or extension URL is safe to emit.
enforceDemoModeGuard() Blocks restricted mutations when running in demo mode.

Htmx exposes one domain hookable method to control when autoExtractTargets or boosted HTMX requests intercept a ProcessWire Page::render response:

Hook Arguments & Return Timing What It Does
Htmx::shouldInterceptPageRender (Page $page): bool After Decides whether Htmx intercepts and extracts partial targets from the rendered Page response. Return false to bypass interception for binary, PDF, or export templates.

Skip HTMX Target Extraction for PDF Templates

Section titled “Skip HTMX Target Extraction for PDF Templates”
site/ready.php
<?php
declare(strict_types=1);
namespace ProcessWire;
$wire->addHookAfter('Htmx::shouldInterceptPageRender', function (HookEvent $event): void {
$page = $event->arguments(0);
if ($page instanceof Page && $page->template->name === 'charter-pdf') {
$event->return = false;
}
});

Scaffolds a new stateful Component class in site/templates/components/ (or a custom directory):

Terminal window
php vendor/bin/wire make:htmx:component CharterFilterComponent --dry-run --json
Flag Description
--dir Custom target directory for the generated component file (defaults to componentsPath).
--dry-run Preview the generated path and code without writing to disk.
--json Output structured JSON result for automation.

Scaffolds a new stateless Ui primitive class in site/templates/ui/ (or a custom directory):

Terminal window
php vendor/bin/wire make:htmx:ui YachtCardUi --dir=site/templates/ui --dry-run --json
Flag Description
--dir Custom target directory for the generated Ui file (defaults to uiPath).
--dry-run Preview the generated path and code without writing to disk.
--json Output structured JSON result for automation.

Inspects, prunes, or clears HtmxCache compiled UI shapes, component schemas, and render caches:

Terminal window
php vendor/bin/wire htmx:cache:clear --stats --json
php vendor/bin/wire htmx:cache:clear --class=YachtCardUi --force --json
php vendor/bin/wire htmx:cache:clear --prune --days=7 --force --json
Flag Description
--class Limit cache clearing to a specific Ui or Component class name.
--prune Remove only stale cache entries older than --days instead of clearing everything.
--days Age threshold in days when used with --prune.
--stats Display cache statistics (file counts, sizes, shapes) without deleting files.
--force Skip interactive confirmation prompt in scripts and CI pipelines.
--json Output structured JSON summary.

SignedStateCodec Throws a Salt Length Error

Section titled “SignedStateCodec Throws a Salt Length Error”

Symptom: Exception stating $config->userAuthSalt is missing or shorter than 16 characters.

Fix: Ensure site/config.php defines a strong $config->userAuthSalt of at least 16 characters. Never rotate it casually without expecting active browser tabs to reload fresh state tokens.

Symptom: Clicking a button calling /hx/req returns 403.

Fix:

  1. Confirm the component is registered via $htmx->registerComponent('alias', MyComponent::class) in site/ready.php.
  2. For mutating actions (POST/PUT/PATCH/DELETE), verify $session->CSRF->renderInput() is inside the form.
  3. For GET/QUERY actions, confirm the PHP method name starts with query (e.g., queryFilter).
  4. Check authorize() and authorizeAction($action) return true for the current user.

Symptom: Changes to a Ui class template do not appear immediately.

Fix: Run php vendor/bin/wire htmx:cache:clear --force (or --class=YachtCardUi) to flush the compiled shape cache in site/assets/cache/Htmx/.

  • 6-Locale Translation Packs: Bundled CSV packs (en, tr, de, fr, es, ru) live in site/modules/Htmx/languages/. Import the matching .csv file under Setup -> Languages -> [Language] -> Site Translation Files.
  • ProCache / CDN Exclusion: Always exclude /hx/req (and any custom endpointUrl) from full-page HTML caches so HKDF-signed hx__state tokens and CSRF fields are never shared across visitors.

Next steps