wordpress 如何返回自定义的REST API响应而不带引号?

bxgwgixi  于 2022-11-02  发布在  WordPress
关注(0)|答案(1)|浏览(228)

假设以下简单的自定义WP REST API端点:

  1. add_action('rest_api_init', function () {
  2. $namespace = 'wp/v2';
  3. $routes = ['products', 'product-categories'];
  4. foreach($routes as $route) {
  5. register_rest_route($namespace, $route, [
  6. 'methods' => WP_REST_Server::READABLE,
  7. 'callback' => 'custom_rest'
  8. ]);
  9. }
  10. });
  11. function custom_rest() {
  12. return new WP_REST_Response('[{}]', 200);
  13. }

如何在函数custom_rest()中返回不带单引号(或双引号)的响应,以便客户端可以将结果取为[{}],而不是'[{}]'"[{}]"?就像API在/posts的情况下返回的那样

xfyts7mz

xfyts7mz1#

我刚刚在这里找到了答案

  1. function custom_rest() {
  2. header("Content-Type: application/json");
  3. echo json_encode([]);
  4. exit();
  5. }

相关问题