我已在修改了教程https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/plugin-sidebar-0/ 满足ES6+的要求,但我很难将其实际保存到帖子中。每当我刷新页面时,该值就会消失。
Addendum: 使用时wp.data.select(\'core/editor\').getEditedPostAttribute(\'meta\')[\'library_plugin_alternate_title\'];
为了验证控制台中元的内容,我成功地返回了"test"
这告诉我,它确实按照预期在编辑器中更新了meta。但是,由于某些原因,该值实际上没有保存到数据库中。我假设它会在wp_postmeta
但我没有看到它这样做。
下面是我在PHP中的当前代码,在plugins_loaded
行动电话:
class RegisterSidebar
{
/**
* RegisterSidebar constructor.
*/
public function __construct()
{
add_action(\'init\', [__CLASS__, \'registerScript\']);
add_action(\'enqueue_block_editor_assets\', [__CLASS__, \'loadScriptsToEditor\']);
add_action(\'enqueue_block_assets\', [__CLASS__, \'loadStylesToEditor\']);
}
/**
* Load the files into the editor.
*/
public static function loadScriptsToEditor(): void
{
wp_enqueue_script(\'plugin-sidebar\');
}
/**
* Load the files into the editor.
*/
public static function loadStylesToEditor(): void
{
wp_enqueue_style(\'plugin-sidebar\');
}
/**
* Adding the script so that the plugin sidebar loads.
*/
public static function registerScript(): void
{
wp_register_script(
\'plugin-sidebar\',
LIBRARY_PLUGIN_URI . \'/assets/js/sidebar.js\',
[\'wp-plugins\', \'wp-edit-post\', \'wp-element\', \'wp-components\', \'wp-compose\']
);
wp_register_style(
\'plugin-sidebar\',
LIBRARY_PLUGIN_URI . \'/assets/css/sidebar.css\'
);
register_meta(\'post\', \'library_plugin_alternate_title\', [
\'show_in_rest\' => true,
\'single\' => true,
\'type\' => \'string\',
]);
}
}
以及使用react在Web包中运行的JavaScript:
import PluginIcon from \'./plugin-icon\';
const { registerPlugin } = window.wp.plugins;
const { PluginSidebar } = window.wp.editPost;
const { TextControl } = window.wp.components;
const { withDispatch, withSelect } = window.wp.data;
const { compose } = window.wp.compose;
let MetaBlockField = compose(
withDispatch((dispatch, props) => {
return {
setMetaFieldValue: (value) => {
dispatch(\'core/editor\').editPost({meta: {[props.fieldName]: value}});
}
};
}),
withSelect((select, props) => {
return {
metaFieldValue: select(\'core/editor\')
.getEditedPostAttribute(\'meta\')
[props.fieldName]
};
})
)(({ metaFieldValue, setMetaFieldValue }) => {
return (
<TextControl
label="Alternate Display Title"
value={ metaFieldValue }
onChange={(content) => setMetaFieldValue(content)}
/>
);
});
registerPlugin(\'plugin-sidebar\', {
render() {
return (
<PluginSidebar
name="plugin-sidebar"
icon={ PluginIcon }
title="Library Plugin Sidebar"
>
<div className="plugin-sidebar-content">
<MetaBlockField fieldName="library_plugin_alternate_title"/>
</div>
</PluginSidebar>
);
}
});
如果你看到我遗漏了什么,请告诉我。非常感谢您提前提供的帮助!
Tests Done After Initial Post:
将元字段名称更改为_library_plugin_alternate_title
并添加\'auth_callback\' => function () { return current_user_can(\'edit_posts\'); }
到register_meta()
函数调用,以防权限问题在保留auth_callback
中的属性register_meta()
呼叫移动了register_meta()
函数调用到RegisterSidebar
构造函数,以便在加载插件时运行。将其返回给registerScript()
方法,当该方法不起作用时