Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active August 16, 2021 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/2e8aa22225779558159a19162c667257 to your computer and use it in GitHub Desktop.
Save Shelob9/2e8aa22225779558159a19162c667257 to your computer and use it in GitHub Desktop.
Create WordPress Sidebar Plugin for meta fields
import { __ } from "@wordpress/i18n";
import { PanelBody } from "@wordpress/components";
import { PluginSidebar } from "@wordpress/edit-post";
import { key } from "@wordpress/icons";
import { registerPlugin } from "@wordpress/plugins";
import { TextControl, CheckboxControl } from "@wordpress/components";
import { useSelect, useDispatch } from "@wordpress/data";
const SideBar = () => {
const { meta_one, meta_two } = useSelect((select) =>
select("core/editor").getEditedPostAttribute("meta")
);
const { editPost } = useDispatch("core/editor", [
meta_one,
meta_two
]);
return (
<PluginSidebar
name="plugin-name"
title={__("Plugin Title")}
icon={key}
>
<PanelBody>
<TextControl
label={"Meta One"}
value={meta_one}
onChange={(newValue) => {
editPost({
meta: {
meta_one: newValue,
meta_two,
},
});
}}
/>
</PanelBody>
</PluginSidebar>
);
};
registerPlugin("plugin-name", {
render: SideBar,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment