Suppose the following simple custom WP REST API endpoint:
add_action('rest_api_init', function () {
$namespace = 'wp/v2';
$routes = ['products', 'product-categories'];
foreach($routes as $route) {
register_rest_route($namespace, $route, [
'methods' => WP_REST_Server::READABLE,
'callback' => 'custom_rest'
]);
}
});
function custom_rest() {
return new WP_REST_Response('[{}]', 200);
}
How to return the response in the function custom_rest()
without single (or double) quotes so that the client can fetch the result as [{}]
instead of '[{}]'
or "[{}]"
? Like the API returns in the case of /posts
I've just found the answer here https://wordpress.stackexchange.com/a/246261
function custom_rest() {
header("Content-Type: application/json");
echo json_encode([]);
exit();
}