| Install | |
|---|---|
composer require desert-dionysus/inertia-wordpress |
|
| Latest Version: | v1.0.2 |
| PHP: | >=8.2 |
A modernized Inertia.js server-side adapter for WordPress, aligned with Inertia.js v2.0+ protocol.
Note: This is a fork of the excellent work by Andrew Rhyand (BoxyBird). While the core philosophy remains the same, this version has been modernized for PHP 8.2+ and adds support for the latest Inertia.js features like Deferred Props, Merge Props, and more.
Install the package via composer:
composer require desert-dionysus/inertia-wordpress
This project is a fork of boxybird/inertia-wordpress. We highly recommend checking out Andrew's original work and examples:
Location:
/wp-content/themes/your-theme/app.php(orlayout.php)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php wp_head(); ?>
</head>
<body>
<?php bb_inject_inertia(); ?>
<?php wp_footer(); ?>
</body>
</html>
inertia() HelperThis fork introduces a global inertia() helper function that provides a fluent API similar to Laravel's adapter.
// Render a component
return inertia('Index', ['posts' => $posts]);
// Chainable methods
return inertia()
->version('1.0.0')
->share('key', 'value')
->render('Index', $props);
use DesertDionysus\Inertia\Inertia;
// Using the Class
return Inertia::render('Posts/Index', [
'posts' => $posts,
]);
// Or using the helper
return inertia('Posts/Index', [
'posts' => $posts,
]);
Shared data is automatically included in every Inertia response.
inertia()->share('site_name', get_bloginfo('name'));
// Shared Closures are only executed if they are included in the response
inertia()->share('auth', function () {
return is_user_logged_in() ? wp_get_current_user() : null;
});
Props wrapped in always() will be included in every response, even during partial reloads where they weren't specifically requested.
return inertia('Profile', [
'user' => $user,
'social_links' => Inertia::always($links),
]);
Deferred props allow you to load heavy data asynchronously after the initial page load.
return inertia('Dashboard', [
'stats' => Inertia::defer(fn() => get_heavy_stats()),
'logs' => Inertia::defer(fn() => get_logs(), 'activity-group'),
]);
Useful for infinite scrolling or pagination.
return inertia('Blog', [
'posts' => Inertia::merge(fn() => get_next_page_posts()),
]);
Handles full-page redirects, even during Inertia AJAX requests.
return Inertia::location('https://external-site.com');
This adapter automatically includes a wp_rest nonce in every response under the nonce prop, simplifying CSRF protection for your API calls.
Easily pass flash messages or validation errors that will be automatically shared via the flash and errors props.
// In your "Controller"
inertia()->flash('success', 'Post updated!');
inertia()->withErrors(['title' => 'Title is required']);
return Inertia::location(get_permalink($post_id));
// Automatically version from Vite manifest
Inertia::versionFromVite(get_stylesheet_directory() . '/dist/manifest.json');
// Or from any specific file
Inertia::versionFromFile(get_stylesheet_directory() . '/style.css');
// Or manually
Inertia::version('v1.2.3');
add_action('init', function () {
Inertia::setRootView('layout.php');
});