tools/rest-route-generator
WordPress REST API Route Generator
Register a custom REST API endpoint with register_rest_route(): the rest_api_init hook, a permission_callback you have to choose, an argument schema WordPress validates for you, and a handler that returns WP_Error or a response.
snippet.php
/**
* Registers the my-plugin/v1/items/(?P<id>\d+) route.
*
* The hook is 'rest_api_init' rather than 'init'. Registering on 'init' runs
* far too early on a REST request and the route never appears.
*/
function wps_register_items_route() {
register_rest_route(
'my-plugin/v1',
'/items/(?P<id>\\d+)',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'wps_items_handler',
'permission_callback' => 'wps_items_permission',
'args' => array(
'id' => array(
'type' => 'integer',
'required' => true,
'description' => 'The item to return.',
),
),
)
);
}
add_action( 'rest_api_init', 'wps_register_items_route' );
/**
* Answers the request.
*
* Read parameters off $request with array access — it merges the URL captures,
* the query string and the JSON body, so the handler does not care where a
* value arrived from.
*
* Return anything JSON-serialisable and WordPress wraps it in a 200. Return a
* WP_Error to choose a different status.
*/
function wps_items_handler( WP_REST_Request $request ) {
$id = $request['id'];
return rest_ensure_response(
array(
'success' => true,
)
);
}
/**
* Who may call this route.
*
* Returning false sends 401 to a logged-out caller and 403 to a logged-in one,
* which WordPress works out on its own. Returning a WP_Error instead lets you
* choose the status and the message.
*/
function wps_items_permission() {
return current_user_can( 'edit_posts' );
}