-
-
Save anver/faa1e4a56e47693fdd8c8f08531eb7d6 to your computer and use it in GitHub Desktop.
Use post meta in gutenberg
This file contains 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