WREST - easy to use fluent REST API wrapper for WordPress.
- PHP >= 5.6
- WordPress >= 4.4
You can install wRest in two ways, via composer and manually.
Add dependency in your project (theme/plugin):
composer require arafatkn/wrest
Now add autoload.php
in your file if you haven't done already.
require __DIR__ . '/vendor/autoload.php';
Not Available Yet.
WordPress API needs a namespace, so you have to set a namespace first.
One way is to set a default namespace before creating routes.
wrest()->setNamespace('my-plugin/v1');
wrest()->get('hello', function() {
return 'Hello world';
});
or you can set namespace for a group of routes.
wrest()->usingNamespace('my-plugin/v1', function($wrest) {
// You can use both $wrest or wrest() here
$wrest->get('greeting', function(WP_REST_Request $req) {
return 'Hello world';
});
});
Passed callback will get a WP_REST_Request
object as a parameter.
wrest()->get('greeting', function(WP_REST_Request $req) {
return 'Hello world';
});
wrest()->get('posts', $callback);
wrest()->post('posts', [$postController, 'store']);
wrest()->put($uri, $callback);
wrest()->patch($uri, $callback);
wrest()->delete($uri, $callback);
wrest()->any($uri, $callback); // All Routes GET, POST, PUT, PATCH, DELETE
wrest()->match(['GET', 'POST'], $uri, $callback);
Passing a capability
wrest()->get('greeting', function() {
return 'Hello world';
})->permission('manage_options');
Passing a callback
wrest()->get('greeting', function() {
return 'Hello world';
})->permission(function(WP_REST_Request $req) {
return is_user_logged_in();
});
wrest()->get('/posts/{slug}', function(WP_REST_Request $request, $slug) {
//
})->param('slug', '[A-Za-z]+');
wrest()->get('/user/{id}/{name}', function ($request, $id, $name) {
//
})->param('id', '[0-9]+')->param('name', '[a-z]+');
wrest()->get('/user/{id}/{name}', function ($request, $id, $name) {
//
})->param(['id' => '[0-9]+', 'name' => '[a-z]+']);
If you do not pass a regex for a param then [^/]+
will be used as default.
wrest()->get('/posts/{slug}', function(WP_REST_Request $request, $slug) {
// Also Works. slug will contain all the characters between posts/ and next /.
});
Action can be a callback, a class method, a static class method or a non-static class method and can be passed as below.
wrest()->get('posts', function() => {});
wrest()->get('posts', 'getAllPosts'); // getAllPosts is a function.
wrest()->get('posts', 'PostController@getAll'); // getAll is static function.
wrest()->get('pages', [$pageController, 'getAll']); // getAll is non-static function.
wrest()->get('authors', [CommentController::class, 'getAll']); // getAll is static function.
All the functions will get a WP_REST_Request
object as a parameter.
- Namespaces for All Routes.
- Normal Routes.
- Routes with Parameters.
- Route Actions.
- Permission Management.
- Add support for route groups.
- Add support for namespace on the fly.
- Add support for namespace groups.
- Add support for resource routes.
- Add support for schema.
- Add support route redirection.
- Add support for passing matched parameters directly to actions.
Special thanks to Tareq Hasan for this awesome idea.
This is still in beta, though I have a confidence that it will work as expected. You can contribute by reporting bugs, fixing bugs, reviewing pull requests and more ways. Go to issues section, and you can start working on a issue immediately. If you want to add or fix something, open a pull request.
The MIT License (MIT). Please see License File for more information.