-
-
Save bikramchettri/e1f82a8c7b7c1dd82f86b80221cee07d to your computer and use it in GitHub Desktop.
Use post meta in gutenberg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://make.wordpress.org/core/2020/03/02/general-block-editor-api-updates/ | |
// https://github.com/WordPress/gutenberg/tree/trunk/packages/core-data | |
import { | |
PanelRow, TextControl, | |
} from '@wordpress/components'; | |
import { useSelect } from '@wordpress/data'; | |
import { useEntityProp } from '@wordpress/core-data'; | |
import { PluginDocumentSettingPanel } from '@wordpress/edit-post'; | |
import { registerPlugin } from '@wordpress/plugins'; | |
function CustomMetaPanel() { | |
const postType = useSelect( | |
(select) => select('core/editor').getCurrentPostType(), | |
[], | |
); | |
const [meta, setMeta] = useEntityProp('postType', postType, 'meta'); | |
const metaValue = meta.customMeta; | |
const updateMetaValue = (newValue) => { | |
setMeta({ ...meta, customMeta: newValue }); | |
}; | |
return ( | |
<PluginDocumentSettingPanel name="customMetaPanel" title="Custom meta"> | |
<PanelRow> | |
<TextControl | |
label="Custom meta" | |
value={metaValue} | |
onChange={updateMetaValue} | |
className="custom-meta-field" | |
/> | |
</PanelRow> | |
</PluginDocumentSettingPanel> | |
); | |
} | |
registerPlugin('plugin-name', { | |
render: CustomMetaPanel, | |
icon: '', | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment