我试着按照Gutenberg Handbook 创建可以更改帖子元数据的块。
虽然当我尝试使用setAttributes
功能来自props
为了保存新的数据,它保留在页面上,但实际上并没有保存回数据库,因为我相信手册中指出,如果它的来源是meta
. 我一定错过了什么,但我找不到可以帮助我的资源。
php:
$args = ...
register_post_type(\'event\', $args);
register_meta(\'event\', \'event_location\', [
\'show_in_rest\' => true,
\'single\' => true,
\'type\' => \'string\'
]);
javascript:registerBlockType(\'my-plugin/event-location\', {
title: \'Event Location\',
category: \'widgets\',
attributes: {
location: {
type: \'string\',
source: \'meta\',
meta: \'event_location\'
}
},
edit ({ className, attributes, setAttributes }) {
const { location } = attributes
function updateContent (e) {
setAttributes({ location: e.target.value })
}
return el(
\'p\',
{ className: className },
el(
\'input\',
{ value: location, onChange: updateContent }
)
)
},
save () {
return null
}
})