Inertia.js Adapter for WordPress and PHP

himanshu-here1 pts0 comments

GitHub - webkul/inertia: Inertia.js server-side adapter for PHP. Handles full visits, Inertia XHR visits, asset-version 409 handshakes and partial reloads. Framework-agnostic, with transparent WordPress integration. · GitHub

/" data-turbo-transient="true" />

Skip to content

Search or jump to...

Search code, repositories, users, issues, pull requests...

-->

Search

Clear

Search syntax tips

Provide feedback

--><br>We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Cancel

Submit feedback

Saved searches

Use saved searches to filter your results more quickly

-->

Name

Query

To see all available qualifiers, see our documentation.

Cancel

Create saved search

Sign in

/;ref_cta:Sign up;ref_loc:header logged out"}"<br>Sign up

Appearance settings

Resetting focus

You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.

Dismiss alert

{{ message }}

webkul

inertia

Public

Notifications<br>You must be signed in to change notification settings

Fork

Star

main

BranchesTags

Go to file

CodeOpen more actions menu

Folders and files<br>NameNameLast commit message<br>Last commit date<br>Latest commit

History<br>4 Commits<br>4 Commits

examples

examples

src

src

vendor

vendor

LICENSE

LICENSE

README.md

README.md

composer.json

composer.json

View all files

Repository files navigation

webkul/inertia

Inertia.js server-side adapter for PHP — the equivalent of what<br>inertia-laravel provides on Laravel, for any plain-PHP application.

It is framework-agnostic: everything runs on native PHP. When WordPress is<br>loaded, its helpers (wp_json_encode, status_header, sanitize_text_field,<br>wp_head / wp_footer in the default shell, the blog_charset option, …)<br>are picked up automatically — no configuration needed.

It handles the full Inertia protocol:

First / standard visit → full HTML document with the page object embedded<br>in a JSON script tag (script[data-page=""][type="application/json"],<br>the Inertia v3 convention).

Inertia visit (XHR with X-Inertia: true) → bare JSON page object, no<br>HTML, so the client swaps props without a page reload.

Stale assets (X-Inertia-Version mismatch on GET) → 409 +<br>X-Inertia-Location, telling the client to do one hard reload to pick up<br>new bundles.

Partial reloads → prop filtering via X-Inertia-Partial-Data /<br>X-Inertia-Partial-Except, with closure props resolved lazily after<br>filtering so skipped props cost no queries.

Installation

composer require webkul/inertia

While the package lives inside a project as a path repository, add to the<br>project's composer.json:

"repositories": [<br>{ "type": "path", "url": "packages/inertia" }<br>],<br>"require": {<br>"webkul/inertia": "@dev"

Usage

Plain PHP

set_version( '1.0.0' ) // string or callable, e.g. a build hash<br>->set_app_id( 'app' ); // container / script id for the default shell

$inertia->render( 'Order', array(<br>'orders' => fn() => fetch_orders(), // lazy: skipped on partial reloads<br>'filters' => $filters,<br>) );">use Webkul\Inertia\Inertia;

$inertia = Inertia::instance()<br>->set_version( '1.0.0' ) // string or callable, e.g. a build hash<br>->set_app_id( 'app' ); // container / script id for the default shell

$inertia->render( 'Order', array(<br>'orders' => fn() => fetch_orders(), // lazy: skipped on partial reloads<br>'filters' => $filters,<br>) );

WordPress (custom HTML shell)

set_version( MY_PLUGIN_SCRIPT_VERSION )<br>->set_root_view( array( My_Template::instance(), 'render_ui_template' ) );

if ( ! $inertia->is_inertia_request() ) {<br>// Assets are only needed for the HTML shell, not for JSON visits.<br>my_plugin_enqueue_app_assets();

$inertia->render( 'Order', $props );">use Webkul\Inertia\Inertia;

$inertia = Inertia::instance()<br>->set_version( MY_PLUGIN_SCRIPT_VERSION )<br>->set_root_view( array( My_Template::instance(), 'render_ui_template' ) );

if ( ! $inertia->is_inertia_request() ) {<br>// Assets are only needed for the HTML shell, not for JSON visits.<br>my_plugin_enqueue_app_assets();

$inertia->render( 'Order', $props );

render() always terminates the request.

Configuration

Method<br>Purpose

set_version( string|callable $version )<br>Asset version used for the 409 stale-asset handshake.

set_root_view( callable $renderer )<br>Renderer for the HTML shell on standard visits. Receives ( string $page_json, array $page ) and must output the full document.

set_app_id( string $id )<br>Container / script id used by the built-in fallback shell (default app). Only relevant when no root view is set.

set_charset( string $charset )<br>Response charset (default UTF-8). Under WordPress the blog_charset option takes precedence.

If no root view is configured, a minimal shell is rendered: a JSON script tag<br>plus an empty "> — and, inside WordPress, wp_head() /<br>wp_footer() / body_class() are included automatically.

Examples

Runnable end-to-end examples live in...

inertia json shell webkul wordpress partial

Related Articles