OUR BLOG

05 Oct 2020

WP 5.3在REST API中支持对象和数组类型

在WordPress 5.3中,register_meta函数(包括register_post_meta)现在支持’object’和’array’数据类型。

例子
以下代码示例注册了一个名为“ release”的对象,该字段接受给定的JSON数据。
{
"meta": {
"release": {
"version": "5.2",
"artist": "Jaco"
}
}
}

register_post_meta(
'post',
'release',
array(
'single' => true,
'type' => 'object',
'show_in_rest' => array(
'schema' => array(
'type' => 'object',
'properties' => array(
'version' => array(
'type' => 'string',
),
'artist' => array(
'type' => 'string',
),
),
),
),
)
);

数组的例子
{
"meta": {
"projects": [
"WordPress",
"BuddyPress"
]
}
}

register_post_meta(
'post',
'projects',
array(
'single' => true,
'type' => 'array',
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
),
)
);

{
"meta": {
"projects": [
{
"name": "WordPress",
"website": "https://wordpress.org"
},
{
"name": "BuddyPress",
"website": "https://buddypress.org"
}
]
}
}

register_post_meta(
'post',
'projects',
array(
'single' => true,
'type' => 'array',
'show_in_rest' => array(
'schema' => array(
'items' => array(
'type' => 'object',
'properties' => array(
'name' => array(
'type' => 'string',
),
'website' => array(
'type' => 'string',
'format' => 'uri',
),
),
),
),
),
)
);

admin