我是react和Gutenberg开发的新手,我正在尝试在文档侧栏中添加一些字段。
虽然我成功地将meta保存为单个值,但我正在努力找到将字段保存为数组的方法。
JS公司
import { __ } from \'@wordpress/i18n\';
import { compose } from \'@wordpress/compose\';
import { registerPlugin } from \'@wordpress/plugins\';
import { PluginDocumentSettingPanel } from \'@wordpress/edit-post\';
import { TextControl } from "@wordpress/components";
import { select, withSelect, withDispatch } from \'@wordpress/data\';
import { Fragment } from \'@wordpress/element\';
function TestControl( { newValue, updateMeta } ) {
return (
<Fragment>
<TextControl
label={ __( \'Field One\', \'test\' ) }
value={ newValue.one }
onChange={ ( value ) => updateMeta( value ) }
/>
<TextControl
label={ __( \'Field Two\', \'test\' ) }
value={ newValue.two }
onChange={ ( value ) => updateMeta( value ) }
/>
</Fragment>
);
}
const FieldsTestControl = compose( [
withSelect( () => {
return {
newValue: select( \'core/editor\' ).getEditedPostAttribute( \'meta\' )._metakey,
};
} ),
withDispatch( ( dispatch ) => ( {
updateMeta( value ) {
dispatch( \'core/editor\' ).editPost(
{ meta: { _metakey:{
\'one\': value.one,
\'two\': value.two,
} } }
);
},
} ) ),
] )( TestControl );
const TestPanel = () => {
return (
<PluginDocumentSettingPanel
name="test"
title="Test"
className="test"
>
<FieldsTestControl />
</PluginDocumentSettingPanel>
);
};
registerPlugin( \'test-panel\', {
render: TestPanel,
icon: \'\',
} );
PHPregister_post_meta( \'post\', \'_metakey\', [
\'type\' => \'object\',
\'single\' => true,
\'auth_callback\' => \'__return_true\',
\'show_in_rest\' => [
\'schema\' => [
\'type\' => \'object\',
\'properties\' => [
\'one\' => [
\'type\' => \'string\',
],
\'two\' => [
\'type\' => \'string\',
],
],
],
],
]);