Search by

concept-image / wp-ajax

concept-image

Secure WordPress AJAX for Acorn themes: an #[Ajax] attribute on action classes or Hooks methods, one dispatcher (nonce always verified, access, capability, rate limit, validation, stable JSON errors), AjaxRequest/AjaxResponse with Blade fragments, nonce refresh for cached pages, an Alpine.js client

Package info

gitlab.com/concept-image-public/wp-ajax

Issues

pkg:composer/concept-image/wp-ajax

Statistics

Installs: 17

Dependents: 3

Suggesters: 1

Stars: 0

0.2.0 2026-12-03 09:47 UTC

This package is auto-updated.

Last update: 2026-12-03 08:48:09 UTC


README

WP Ajax

Secure WordPress AJAX for Acorn themes

An #[Ajax] attribute on an action class or a Hooks method, one dispatcher that always verifies WordPress' nonce, checks access, capability and rate limit (per user, or per client IP behind trusted proxies) and answers stable JSON errors, typed request getters with Laravel validation, responses with Blade fragments, a nonce refresh for cached pages, and a small Alpine.js client (loading states, Alpine.morph, field errors, AJAX listings with filters, sort, pagination and URL sync, and a navigation mode for archives: term links and GET filters swapped in place, like wire:navigate) with five Blade components. No wp_ajax_ hooks, check_ajax_referer(), $_POST, wp_send_json() or jQuery to write.

Installation

composer require concept-image/wp-ajax
wp acorn ajax:install      # client into resources/scripts/ajax, @alpinejs/morph into package.json
npm install && npm run build

Requires PHP 8.4, roots/acorn ^6.4 and concept-image/wp-hook-attributes ^0.1. Acorn discovers the service provider. Add the lines ajax:install prints to resources/scripts/main.ts.

In 30 seconds

// app/Actions/SubscribeToNewsletter.php: found automatically, `wp acorn make:ajax SubscribeToNewsletter`
namespace App\Actions;

use App\Services\Newsletter;
use ConceptImage\WpAjax\Attributes\Ajax;
use ConceptImage\WpAjax\Http\AjaxRequest;
use ConceptImage\WpAjax\Http\AjaxResponse;

#[Ajax(throttle: '5,1')]                       // ci_subscribe_to_newsletter, guests + logged in, POST, nonce verified
final class SubscribeToNewsletter
{
    public function handle(AjaxRequest $request, Newsletter $newsletter): AjaxResponse
    {
        $request->validate(['email' => ['required', 'email']]);   // 422 + messages per field
        $newsletter->subscribe($request->email('email'));

        return AjaxResponse::message(__('Thanks!', 'ci-replace'));
    }
}
<x-ajax::form :action="App\Actions\SubscribeToNewsletter::class" reset>
  <input type="email" name="email" required>
  <p x-text="$ajaxErrors.email"></p>
  <button class="in-data-loading:opacity-50">{{ __('Subscribe', 'ci-replace') }}</button>
</x-ajax::form>

Every refusal is JSON through wp_send_json_error(): 405 ajax_method_not_allowed, 403 ajax_invalid_nonce, 401 ajax_unauthenticated, 403 ajax_forbidden, 429 ajax_too_many_requests, 422 ajax_invalid, 500 ajax_error (logged; details only in debug). Never 0.

Documentation

PageCovers
Getting startedInstall, ajax:install, a first action, config
Actions#[Ajax] on action classes and Hooks methods, parameters, Access, default names, declaration errors, ajax:list
Requests and validationAjaxRequest getters, validate(), files, user, language
Responses and fragmentsAjaxResponse, fragments, events, redirects, error codes and messages
Client and directives$ajax(), x-ajax, x-ajax-listing (filters, sort, pagination, URL, back/forward, data-ajax-base term links), x-ajax-navigate (archive navigation: term links, GET filters, full-page cache), regions and morphing, loading states, $ajaxErrors, events, nonces
Blade components<x-ajax::form>, <x-ajax::button>, <x-ajax::listing>, <x-ajax::navigate>, <x-ajax::region>
SecurityOrder of the checks, native nonces and full-page cache, access, throttle and trusted_proxies, escaping, CSP
RecipesNewsletter, load more, filtered listing with pagination and URL sync, archive with term links in form mode (data-ajax-base), archive with term links and filters (navigation mode), category paths and checkbox filters, live search and autocomplete, favourites, WooCommerce mini-cart

Commands

  • wp acorn ajax:list: declared actions (access, capability, methods, throttle, nonce, source).
  • wp acorn ajax:install {--force}: publishes or updates the client in the theme.

AI files

The package ships a Claude Code skill and rules for sites: wp acorn vendor:publish --tag=wp-ajax-ai (or wp-starter-theme's wp acorn starter:ai) publishes them to the Bedrock root's .claude/.

Development

composer ci   # Pint, Larastan (level max), Pest

The TypeScript client type-checks with tsc --noEmit -p resources/scripts (strict, alpinejs + @types/alpinejs); its pure helpers (URLs, link eligibility, form serialization, region pairing) have unit tests: node --test tests/Client/ (Node 23.6+). See the CHANGELOG. Inspired by Pollora/ajax (MIT: nonce by default, error codes, order of the checks), Livewire (wire:loading, targets, morphing, wire:navigate) and htmx (hx-boost, hx-select).

License

MIT. See LICENSE.

Credits