Gutenberg custom setting panel not saving meta data
-
Hi all,
I try to update and save metadata using the
PluginDocumentSettingPanel
first I registered the custom meta field in PHP. The settings panel is working, but the value won’t save.Here is my PHP code:
function commpro_register_meta_values(){ register_meta( 'post', '_custom_header_image', [ 'show_in_rest' => true, 'single' => true, 'type' => 'string', 'auth_callback' => function() { return current_user_can( 'edit_posts' ); } ]); } function commpro_register_script(){ wp_enqueue_script( 'commpro-meta-plugin', plugin_dir_url(__FILE__) . '/build/script.js', [ 'wp-edit-post', 'wp-element', 'wp-components', 'wp-plugins', 'wp-data' ], false, false ); } add_action('init', 'commpro_register_meta_values'); add_action('enqueue_block_editor_assets', 'commpro_register_script');
And here are the JS files that are compiled into script.js
const { registerPlugin } = wp.plugins; import CommproCustomSidebarItem from './panel.js'; registerPlugin( 'commpro-custom-sidebar-item', { render: CommproCustomSidebarItem, icon: '', } );
const { compose } = wp.compose; const { withSelect, withDispatch } = wp.data; const { PanelRow, TextControl } = wp.components; const { PluginDocumentSettingPanel } = wp.editPost; const CommproCustomSidebarItem = ( {postMeta, postType, setPostMeta} ) => { if (postType !== 'testimonial') return null; return( <PluginDocumentSettingPanel name="custom-header" title="Aangepaste header afbeelding" className="custom-header" > <PanelRow> <TextControl label="Dit is een aangepast veld" value={ postMeta._custom_header_image } onChange={ ( value ) => setPostMeta( { _custom_header_image: value } )} /> </PanelRow> </PluginDocumentSettingPanel> ) }; export default compose([ withSelect( (select) => { return { postMeta: select( 'core/editor' ).getEditedPostAttribute( 'meta' ) || {}, postType: select( 'core/editor' ).getCurrentPostType() }; }), withDispatch( ( dispatch ) => { return { setPostMeta( newMeta ){ console.log(newMeta); dispatch( 'core/editor' ).editPost( { meta: newMeta } ); } } }) ]) (CommproCustomSidebarItem);
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Gutenberg custom setting panel not saving meta data’ is closed to new replies.