I am trying to follow the instruction in the Gutenberg Handbook to create a block that can change meta data of a post.
Though when I try to use the setAttributes
function from props
to save the new data it persisted on the page, but is not actually saved back to the database, as I believe the Handbook states it should if it's source is meta
. I must be missing something, but I cannot find a resource to help.
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
}
})