Skip to content

Rest API

config/rest-api.php

This file contains the configuration for the rest api. You can enabled/disable the rest api (theme routes) and register custom routes.

This option enables/disables the rest api. If you disable the rest api, the theme routes will not be registered. Default is false.

This option allows you to register custom routes. To register a route, you need to add a new array item to the routes array. This is how the configuration works:

config/rest-api.php
<?php
'routes' => [
'namespace' => [
'route' => [
'methods' => HTTPMethods::GET,
'callback' => 'callback_function',
]
]
]

For the callback function, you can use a anonymous function, a function name or a class method (recommended).

You can also add a permission_callback to the route. This callback will be called before the route callback is called.

config/rest-api.php
'route' => [
'methods' => HTTPMethods::GET,
'callback' => 'callback_function',
'permission_callback' => function () {
return current_user_can('edit_posts');
}
];

Read more about this here: Permissions callback

You can also add arguments to the route. These arguments will be passed to the callback function.

config/rest-api.php
'route' => [
'methods' => HTTPMethods::GET,
'callback' => 'callback_function',
'args' => [
'id' => [
'required' => true,
'validate_callback' => function ($param, $request, $key) {
return is_numeric($param);
}
]
]
];

Read more about this here: Arguments