Skip to content

Instantly share code, notes, and snippets.

@nikolov-tmw
Created April 22, 2024 10:19
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 nikolov-tmw/fed474cd80ac48be99680492d05688ed to your computer and use it in GitHub Desktop.
Save nikolov-tmw/fed474cd80ac48be99680492d05688ed to your computer and use it in GitHub Desktop.
import { Fragment } from "@wordpress/element";
import { createHigherOrderComponent } from "@wordpress/compose";
import { addFilter } from "@wordpress/hooks";
import { TextControl } from "@wordpress/components";
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { useBlockProps, PlainText } from '@wordpress/block-editor';
const AddSubtitle = createHigherOrderComponent((BlockEdit) => {
const AddSubtitle = (props) => {
if (props.name !== 'core/post-title') {
return <BlockEdit {...props} />;
}
const { getCurrentPostType, getCurrentPostId } = useSelect('core/editor');
const postType = getCurrentPostType();
if (!postType) {
return <BlockEdit {...props} />;
}
const [meta, setMeta] = useEntityProp(
'postType',
postType,
'meta',
props.context.postId
);
const updateSubtitle = (newValue) => {
setMeta({...meta, kia_subtitle: newValue});
};
const TagName = 'h2';
return (
<Fragment>
<BlockEdit {...props} />
<PlainText
tagName={TagName}
placeholder="No Subtitle"
value={meta.kia_subtitle}
onChange={updateSubtitle}
__experimentalVersion={ 2 }
{...useBlockProps()}
/>
</Fragment>
);
};
return AddSubtitle;
}, 'AddSubtitle');
addFilter(
'editor.BlockEdit',
'addsubtitle-test/add-subtitle',
AddSubtitle
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment