I have registered settings and need it to be showed in REST API.
$args = [
'show_in_rest' => true
];
register_setting('default_sidebars', 'default_sidebars', $args);
When I save a single value and then request data on endpoint /wp-json/wp/v2/settings
, everything works perfectly.
But the problem is that I save serialized data like this.
a:2:{s:4:"post";s:7:"general";s:4:"blog";s:9:"sidebar_1";}
Now I would expect in response something like this:
default_sidebars: {
post: "general",
blog: "sidebar_1"
}
But instead I got default_sidebars: null
.
What should I do to get my data in REST API?
JSON Schema
It’s supported if you explicitly register the object JSON schema as:
$args = array(
'show_in_rest' => array(
'schema' => array(
'type' => 'object',
'properties' => array(
'post' => array(
'type' => 'string',
),
'blog' => array(
'type' => 'string',
),
)
),
),
);
register_setting( 'default_sidebars', 'default_sidebars', $args );
Resulting in the following object:
default_sidebars: {
post: "general",
blog: "sidebar_1"
}
in /wp-json/wp/v2/settings
for the serialized option data:
a:2:{s:4:"post";s:7:"general";s:4:"blog";s:9:"sidebar_1";}
See similar object schema support for register_meta()
in 5.3:
WP 5.3 Supports Object and Array Meta Types in the REST API