以下是向特征图像区域添加新组件的示例。这是从官方文档中获取的,并更新为使用JSX。我正在使用@wordpress/scripts
包来构建此。
// Import from the hooks package
import { addFilter } from \'@wordpress/hooks\';
// Some custom component to add.
const CustomComponent = ({ id }) => {
return id ? (
<h4>You selected an image, yay!</h4>
) : (
<div>You have not selected anything</div>
);
};
// Function to be called by the hook that returns a function containing the new UI
function replacePostFeaturedImage(OriginalComponent) {
return (props) => {
const { featuredImageId } = props;
return (
<div>
<CustomComponent id={featuredImageId} />
<OriginalComponent {...props} />
</div>
);
};
}
// Register the hook.
addFilter(
\'editor.PostFeaturedImage\',
\'my-plugin/replace-post-featured-image\',
replacePostFeaturedImage
);
要更换整个组件,只需拆下
<OriginalComponent />
从
replacePostFeaturedImage
作用
希望它有帮助,并高兴地添加任何进一步的背景需要!