REST API
This document explains how to create and manage REST APIs in WP Bones. It introduces the api.php configuration file in the config folder, which includes settings for the embedded WordPress REST API and custom endpoints. It provides steps to configure authentication and create custom APIs by creating a new folder in the /api directory. The document also highlights the ability to change the folder name by updating the path in the api.php configuration file.
Overview
WP Bones provides a simple way to create your own REST API. You may use the WordPress REST API as well.
Configuration
First of all, we have added a new api.php configuration file to the config folder.
You’ll find some useful settings to handle the embedded WordPress REST API and your custom endpoints.
Create your first API
To create your custom API, you have to create a new folder in /api at the root of your plugin.
The main folder will be the vendor (the name of your project). Typically, within the vendor folder, you will also create the version folder.
For instance, you could create
Routing
Now, we can start creating the REST API route by adding a new file in our structure.
For example, we’ll use route.php
Let’s see a simple implementation of route.php.
You may use your browser to test /wp-json/vendor/v1/example route.
Who can call a route
WordPress asks every REST route for a permission_callback, a function that answers whether the current request may run it. Pass '__return_true' for a route that is public on purpose, as above, or a check of your own:
Since v3.0.0 a route without one refuses every request: it answers WordPress’s own rest_forbidden error, with a 401 status to a visitor and a 403 to a logged-in user, and its callback never runs.
WP Bones also says so with a _doing_it_wrong() notice that names the route. With WP_DEBUG on, WordPress sends the notice as an X-WP-DoingItWrong response header during a REST request (one header, the last notice, when several routes lack the option); when the REST server starts inside another request, the block editor’s for example, it goes to debug.log.
The examples on the rest of this page are public, with '__return_true', to stay short.
HTTP Methods
The Route class supports the following HTTP methods: GET, POST, PUT, PATCH, DELETE
In short, the same supported by the WordPress REST API and defined in the WP_REST_Server class.
Instead of using the static method Route::get(), Route::post(), and so on, you can use ::request() instead. You will be able to use multiple HTTP verbs at the same time.
WP_REST_Request injection
You will be able to get the WP_REST_Request object from the function used in the Route class.
JSON Response
Of course, you may return a JSON response by using the WordPress wp_send_json() function.
You may also use the response() static method provided by the Route class.
Error Response
The best practice is to return a WP_Error object.
Also in this case, you may use the responseError() static method provided by the Route class.
RestController
Of course, you may use a RestController to handle the request. WPBones provides a simple way to create a new controller.
You may create your own controller extending the RestController class.
For example, let’s create a new controller called WPKirkV1Controller in the wp-content/plugins/WPKirk/plugin/API/WPKirkV1Controller.php folder.
Using the php bones make:api Bones command, we can quickly create a RestController.
Now, in the route.php file, you may use the WPKirkV1Controller.
WP_REST_Request and vendor injection
By using a RestController, you can access the WP_REST_Request object and the vendor information in different ways. The first method involves using the request property of the RestController class.
Of course, feel free to check the WordPress documentation for more information about the WP_REST_Request object.
The second way is using the inject object in the method directly.
The vendor information, instead, is available in the property vendor of the RestController class. You will get all paths along with any version information. For instance, wpkirk/v1 in our example.
Authentication
WP Bones will add the Basic Authentication features without using the official WordPress Plugin. The Basic Authentication is based on the WordPress Basic Authentication and the WordPress Basic Authentication Plugin.
Remember that you have to enable this feature from the api.php configuration file.
Options
You may add additional options to the Route following the WordPress REST API.
In the above example, you’ll receive
Of course, you may do the same with the RestController as well
Disable the WordPress REST API
You should not disable the REST API; doing so will break WordPress Admin functionality that depends on the API being active. However, you may use a filter to require that API consumers be authenticated, which effectively prevents anonymous external access.
Anyway, you can require authentication for all REST API requests by setting the require_authentication to true in the configuration file.