kaiseki / wp-rest-api
Register WordPress REST API routes from typed, container-built route definitions
Requires
- php: ^8.2
- kaiseki/config: ^2.0
- kaiseki/wp-hook: ^2.0
- psr/container: ^1.1 || ^2.0
- thecodingmachine/safe: ^1.3 || ^2.0
Requires (Dev)
- bnf/phpstan-psr-container: ^1.1
- kaiseki/php-coding-standard: ^1.0
- maglnet/composer-require-checker: ^4.0
- php-stubs/wordpress-stubs: ^6.2
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^11.0
- roave/security-advisories: dev-latest
- roots/wordpress-core-installer: *
- roots/wordpress-no-content: @stable
- szepeviktor/phpstan-wordpress: ^2.0
- thecodingmachine/phpstan-safe-rule: ^1.4
This package is auto-updated.
Last update: 2026-06-02 23:44:48 UTC
README
Register WordPress REST API routes from typed, container-built route definitions.
Declare your routes in config and implement small, type-safe callback classes instead of hand-wiring
register_rest_route() on rest_api_init. RestRouteRegistry (a kaiseki/wp-hook provider) reads
the rest_api config, resolves each callback from the container, and registers the routes for you.
Installation
composer require kaiseki/wp-rest-api
Requires PHP 8.2 or newer.
Usage
Implement a route callback (and optionally a permission callback):
use Kaiseki\WordPress\RestApi\RestRouteCallbackInterface; use WP_REST_Request; use WP_REST_Response; final class GetThings implements RestRouteCallbackInterface { public function __invoke(WP_REST_Request $request): WP_REST_Response { return new WP_REST_Response(['things' => []]); } }
Register ConfigProvider, then declare routes under the rest_api config key:
use Kaiseki\WordPress\RestApi\RestRoutePermissionCallbackInterface; use WP_REST_Server; return [ 'rest_api' => [ 'namespace' => 'my-plugin/v1', 'route_configs' => [ 'things' => [ 'methods' => WP_REST_Server::READABLE, 'callback' => GetThings::class, // a callable-string, or a RestRoutePermissionCallbackInterface class-string 'permission_callback' => '__return_true', 'args' => [], // optional per-route namespace override ], ], ], ];
On rest_api_init, RestRouteRegistry registers my-plugin/v1/things. Each route_configs entry
maps a route path to its methods, callback, permission_callback, optional args, and an
optional namespace override. Permission callbacks are either a callable-string (e.g.
'__return_true') or a class implementing RestRoutePermissionCallbackInterface
(__invoke(WP_REST_Request): WP_Error|bool).
Routes can also be supplied as ready-made RestRouteInterface objects via the rest_api.routes
config key (container class-strings), in addition to — or instead of — route_configs.
Development
composer install
composer check # check-deps, cs-check, phpstan
License
MIT — see LICENSE.