REST API 文档

💡 云策文档标注

概述

本文档是 WordPress REST API 的开发者参考,介绍了 API 的组织结构、核心特性和可用端点。API 基于 REST 原则设计,使用 JSON 格式和 HTTP 特性,支持公开和私有数据访问。

关键要点

  • API 围绕 REST 设计,使用可预测的资源导向 URL 和 HTTP 响应码处理错误。
  • 采用 JSON 作为请求和响应格式,支持 HAL 标准的 ._links 和 ._embedded 属性,可通过超链接发现。
  • 提供公开匿名访问和私有认证访问,支持内容管理操作,适用于构建替代仪表板、插件增强或单页应用。
  • API 是分布式的,每个支持站点独立提供,无需单一根端点,支持通过索引端点或 OPTIONS 请求进行自发现。
  • 列出了核心端点资源及其基础路由,如文章、页面、分类、用户等。

📄 原文内容

The WordPress REST API is organized around REST, and is designed to have predictable, resource-oriented URLs and to use HTTP response codes to indicate API errors. The API uses built-in HTTP features, like HTTP authentication and HTTP verbs, which can be understood by off-the-shelf HTTP clients, and supports cross-origin resource sharing to allow you to interact securely with the API from a client-side web application.

The REST API uses JSON exclusively as the request and response format, including error responses. While the REST API does not completely conform to the HAL standard, it does implement HAL’s ._links and ._embedded properties for linking API resources, and is fully discoverable via hyperlinks in the responses.

The REST API provides public data accessible to any client anonymously, as well as private data only available after authentication. Once authenticated the REST API supports most content management actions, allowing you to build alternative dashboards for a site, enhance your plugins with more responsive management tools, or build complex single-page applications.

This API reference provides information on the specific endpoints available through the API, their parameters, and their response data format.

REST API Developer Endpoint Reference

Resource Base Route
Posts /wp/v2/posts
Post Revisions /wp/v2/posts/<id>/revisions
Categories /wp/v2/categories
Tags /wp/v2/tags
Pages /wp/v2/pages
Page Revisions /wp/v2/pages/<id>/revisions
Comments /wp/v2/comments
Taxonomies /wp/v2/taxonomies
Media /wp/v2/media
Users /wp/v2/users
Post Types /wp/v2/types
Post Statuses /wp/v2/statuses
Settings /wp/v2/settings
Themes /wp/v2/themes
Search /wp/v2/search
Block Types /wp/v2/block-types
Blocks /wp/v2/blocks
Block Revisions /wp/v2/blocks/<id>/autosaves/
Block Renderer /wp/v2/block-renderer
Block Directory Items /wp/v2/block-directory/search
Plugins /wp/v2/plugins

A Distributed API

Unlike many other REST APIs, the WordPress REST API is distributed and available individually on each site that supports it. This means there is no singular API root or base to contact; instead, we have a discovery process that allows interacting with sites without prior contact. The API also exposes self-documentation at the index endpoint, or via an OPTIONS request to any endpoint, allowing human- or machine-discovery of endpoint capabilities.