插件开发文档

💡 云策文档标注

概述

WordPress 4.4 引入了 REST API 的基础设施,提供了一种简便的数据输入输出方式。通过向 REST API 服务器发送 HTTP 请求,可以检索和存储数据,并利用不同的 HTTP 方法进行操作。

关键要点

  • REST API 使用 HTTP 方法:GET 用于检索数据,POST 用于创建资源(如用户、文章、分类法),PUT 用于更新资源,DELETE 用于删除资源,OPTIONS 用于提供资源上下文。
  • 资源是单个实体或对象,例如 WordPress 中的文章,具有标题和内容等属性,API 响应以字段形式展示这些属性。
  • REST API 支持以新方式与文章和其他 WordPress 资源交互,便于内容共享和结构化处理复杂交互。

📄 原文内容

WordPress 4.4 introduced the infrastructure for a REST API.  The REST API provides an easy way to get data into and out of WordPress.  Data can be retrieved and stored by sending HTTP requests to the REST API server.  The REST API takes advantage of different HTTP methods.

  • GET should be used for retrieving data from the API.
  • POST should be used for creating new resources (i.e users, posts, taxonomies).
  • PUT should be used for updating resources.
  • DELETE should be used for deleting resources.
  • OPTIONS should be used to provide context about our resources.

A resource is any single entity or object.  A good example of a resource for WordPress would be a post. A post has different properties like its title and content.  A response from the API could show us title and content as fields in the response.  The REST API enables us to interact with posts and other WordPress resources in a new way.  The REST API makes sharing our content with the rest of the web easier, and it provides a structured way to handle complex interactions within WordPress.

In this chapter of the Plugin Handbook, we will explore how the API works and how we can leverage its power to do great things with WordPress!