简体   繁体   English

Wordpress REST API 问题

[英]Wordpress REST API Issue

I'm currently trying to wrap my head around Wordpress and its relation to the REST API.我目前正试图围绕 Wordpress 及其与 REST API 的关系。 All of theses issues are only occurring in the context of plugin development.所有这些问题都只发生在插件开发的背景下。 So far I had setup my endpoint and written my callback which will be executed whenever someone is sending a GET-Request to said endpoint.到目前为止,我已经设置了我的端点并编写了我的回调,只要有人向所述端点发送 GET-Request,就会执行该回调。 However, my callback is never called.但是,我的回调从未被调用。 This is the file which I've written so far.这是我迄今为止编写的文件。

  /*
   * Plugin Name: cola-learning
  */

  if(!defined('ABSPATH')){
      die;
  }

  if(!function_exists('add_action')){
      echo 'You cant access this ressource!';
      exit;
  }

  function PrintRESTResponse()
  {
      return rest_ensure_response("student");
  }

  function SetupREST()
  {
      return register_rest_route("student/v1","/view/",[
          'methods' => 'GET',
          'callback' => 'PrintRESTResponse'
      ],false);
  } 

  add_action('rest_api_init','SetupREST');

Perhaps it might also help, if I'll give some background information about my development machine: - OS: Windows 10 - Server: Apache Web Server (included inside XAMPP) - Wordpress Version: 5.3.2 - PHP Version: 7.4 - Development IDE: Eclipse 2019-12 CDT ( with PHP Plugin )如果我提供一些关于我的开发机器的背景信息,也许它也可能有所帮助: - 操作系统:Windows 10 - 服务器:Apache Web 服务器(包含在 XAMPP 中) - Wordpress 版本:5.3.2 - PHP 版本:7.4 - 开发 IDE :Eclipse 2019-12 CDT(带PHP插件)

From my research, everything should work fine.根据我的研究,一切正常。 However, it doesnt :/ Did I miss something crucial?但是,它没有:/我错过了一些重要的东西吗?

Update:更新:

WordPress REST API Routing WordPress REST API 路由

WordPress is having a default route for all request which are directed to the REST API ( at least if you use XAMPP with the Bitnami WordPress application module). WordPress 对所有定向到 REST API 的请求都有一个默认路由(至少如果您将 XAMPP 与 Bitnami WordPress 应用程序模块一起使用)。 Now, if one wants to send a request to said REST API, the person needs to use an URL with the form of ip:port/wp-json/rest_route .现在,如果要向所述 REST API 发送请求,则该人需要使用ip:port/wp-json/rest_route形式的 URL。 ip ressembles the ip address of the server which is hosting wordpress. ip类似于托管 wordpress 的服务器的 ip 地址。 port is the port of said server. port是所述服务器的端口。 wp-json , however, is the portion which differs an ordinary request from a request to the REST API.但是, wp-json是将普通请求与对 REST API 的请求不同的部分。 Everything after this portion ( rest_route ) is the rest route which I've defined in the above source code.这部分( rest_route )之后的所有内容都是我在上面的源代码中定义的休息路线。

What went wrong?什么地方出了错?

My request was pointed to the wrong endpoint.我的请求指向错误的端点。 Therefore I used the URL 'localhost:wpPort/wordpress/student/v1/view/'.因此我使用了 URL 'localhost:wpPort/wordpress/student/v1/view/'。 However, the 'wp-json/' portion is missing.但是,缺少“wp-json/”部分。 Therefore WordPress will search for a page that doesn't exist in the first place.因此,WordPress 将首先搜索一个不存在的页面。 Instead I should have used the URL 'localhost:wpPort/wordpress/wp-json/student/v1/view/'.相反,我应该使用 URL 'localhost:wpPort/wordpress/wp-json/student/v1/view/'。

After some troubleshooting it turns out the URL being used to test was missing a part of the endpoint path.经过一些故障排除后,结果证明用于测试的 URL 缺少端点路径的一部分。

Here is the correct path to test the newly registered endpoint.这是测试新注册端点的正确路径。

localhost:80/wordpress/wp-json/student/v1/view/

Here's some more reading on WP Rest API endpoints:以下是有关 WP Rest API 端点的更多信息:

https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/ https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM