REST API 文档

💡 云策文档标注

概述

本文档详细介绍了 WordPress 区块类型的 API 架构和端点,包括区块类型记录的字段定义和 REST API 端点,用于检索特定区块类型信息。

关键要点

  • 区块类型架构定义了多个字段,如 api_version、title、name、description、icon、attributes、provides_context、uses_context、selectors、supports、category、is_dynamic、editor_script_handles、script_handles、view_script_handles、editor_style_handles、style_handles、styles、variations、textdomain、parent、ancestor、keywords、example,以及已弃用的字段如 editor_script、script、view_script、editor_style、style。
  • 所有字段均为只读,JSON 数据类型包括整数、字符串、对象、数组、布尔值或 null,上下文为 embed、view 或 edit。
  • 提供三个 REST API 端点:GET /wp/v2/block-types 用于检索所有区块类型,GET /wp/v2/block-types/<namespace> 用于按命名空间检索,GET /wp/v2/block-types/<namespace>/<name> 用于按命名空间和名称检索特定区块类型。
  • 端点参数包括 context(默认 view,可选 embed 或 edit)、namespace 和 name,用于控制响应字段和筛选结果。

代码示例

GET /wp/v2/block-types
$ curl https://example.com/wp-json/wp/v2/block-types

GET /wp/v2/block-types/<namespace>
$ curl https://example.com/wp-json/wp/v2/block-types/<namespace>

GET /wp/v2/block-types/<namespace>/<name>
$ curl https://example.com/wp-json/wp/v2/block-types/<namespace>/<name>

📄 原文内容

Schema

The schema defines all the fields that exist within a block type record. Any response from these endpoints can be expected to contain the fields below unless the `_filter` query parameter is used or the schema field only appears in a specific context.

api_version Version of block API.

JSON data type: integer

Read only

Context: embed, view, edit

title Title of block type.

JSON data type: string

Read only

Context: embed, view, edit

name Unique name identifying the block type.

JSON data type: string

Read only

Context: embed, view, edit

description Description of block type.

JSON data type: string

Read only

Context: embed, view, edit

icon Icon of block type.

JSON data type: string or null

Read only

Context: embed, view, edit

attributes Block attributes.

JSON data type: object or null

Read only

Context: embed, view, edit

provides_context Context provided by blocks of this type.

JSON data type: object

Read only

Context: embed, view, edit

uses_context Context values inherited by blocks of this type.

JSON data type: array

Read only

Context: embed, view, edit

selectors Custom CSS selectors.

JSON data type: object

Read only

Context: embed, view, edit

supports Block supports.

JSON data type: object

Read only

Context: embed, view, edit

category Block category.

JSON data type: string or null

Read only

Context: embed, view, edit

is_dynamic Is the block dynamically rendered.

JSON data type: boolean

Read only

Context: embed, view, edit

editor_script_handles Editor script handles.

JSON data type: array

Read only

Context: embed, view, edit

script_handles Public facing and editor script handles.

JSON data type: array

Read only

Context: embed, view, edit

view_script_handles Public facing script handles.

JSON data type: array

Read only

Context: embed, view, edit

editor_style_handles Editor style handles.

JSON data type: array

Read only

Context: embed, view, edit

style_handles Public facing and editor style handles.

JSON data type: array

Read only

Context: embed, view, edit

styles Block style variations.

JSON data type: array

Read only

Context: embed, view, edit

variations Block variations.

JSON data type: array

Read only

Context: embed, view, edit

textdomain Public text domain.

JSON data type: string or null

Read only

Context: embed, view, edit

parent Parent blocks.

JSON data type: array or null

Read only

Context: embed, view, edit

ancestor Ancestor blocks.

JSON data type: array or null

Read only

Context: embed, view, edit

keywords Block keywords.

JSON data type: array

Read only

Context: embed, view, edit

example Block example.

JSON data type: object or null

Read only

Context: embed, view, edit

editor_script Editor script handle. DEPRECATED: Use `editor_script_handles` instead.

JSON data type: string or null

Read only

Context: embed, view, edit

script Public facing and editor script handle. DEPRECATED: Use `script_handles` instead.

JSON data type: string or null

Read only

Context: embed, view, edit

view_script Public facing script handle. DEPRECATED: Use `view_script_handles` instead.

JSON data type: string or null

Read only

Context: embed, view, edit

editor_style Editor style handle. DEPRECATED: Use `editor_style_handles` instead.

JSON data type: string or null

Read only

Context: embed, view, edit

style Public facing and editor style handle. DEPRECATED: Use `style_handles` instead.

JSON data type: string or null

Read only

Context: embed, view, edit

Retrieve a Block Type

Definition & Example Request

GET /wp/v2/block-types

Query this endpoint to retrieve a specific block type record.

$ curl https://example.com/wp-json/wp/v2/block-types

Arguments

context Scope under which the request is made; determines fields present in response.

Default: view

One of: view, embed, edit

namespace Block namespace.

Retrieve a Block Type

Definition & Example Request

GET /wp/v2/block-types/<namespace>

Query this endpoint to retrieve a specific block type record.

$ curl https://example.com/wp-json/wp/v2/block-types/<namespace>

Arguments

context Scope under which the request is made; determines fields present in response.

Default: view

One of: view, embed, edit

namespace Block namespace.

Retrieve a Block Type

Definition & Example Request

GET /wp/v2/block-types/<namespace>/<name>

Query this endpoint to retrieve a specific block type record.

$ curl https://example.com/wp-json/wp/v2/block-types/<namespace>/<name>

Arguments

name Block name.
namespace Block namespace.
context Scope under which the request is made; determines fields present in response.

Default: view

One of: view, embed, edit