Skip to content

Instantly share code, notes, and snippets.

@karstengresch
Forked from vralle/index.js
Created September 6, 2023 09:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karstengresch/aba028584456aedc42ed515adaeeb6ab to your computer and use it in GitHub Desktop.
Save karstengresch/aba028584456aedc42ed515adaeeb6ab to your computer and use it in GitHub Desktop.
Use post meta in gutenberg
// 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