【发布时间】:2021-04-27 15:58:35
【问题描述】:
我正在寻找一种方法来根据用户角色在 Gutenberg 编辑器侧边栏中隐藏一些页面设置面板。由于这些面板标题没有特定的类,我无法使用 CSS 来定位它们,所以我想知道是否有办法至少控制哪些面板在首选项中处于活动状态:
有谁知道我是否可以使用挂钩或其他东西来根据用户角色有条件地预设这些切换?
非常感谢。
【问题讨论】:
标签: wordpress wordpress-gutenberg wordpress-hook
我正在寻找一种方法来根据用户角色在 Gutenberg 编辑器侧边栏中隐藏一些页面设置面板。由于这些面板标题没有特定的类,我无法使用 CSS 来定位它们,所以我想知道是否有办法至少控制哪些面板在首选项中处于活动状态:
有谁知道我是否可以使用挂钩或其他东西来根据用户角色有条件地预设这些切换?
非常感谢。
【问题讨论】:
标签: wordpress wordpress-gutenberg wordpress-hook
我在 WordPress 5.9 上做了一些工作(没有在早期版本上测试)。
从插件/主题中包含 JS
add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'so67286587EnqueueGutenbergEditorAssets' ] );
function so67286587EnqueueGutenbergEditorAssets() {
wp_enqueue_script( 'so67286587-gutenberg', plugins_url( '/js/gutenberg.jsx.js', __FILE__ ), [ 'wp-edit-post' ] );
}
这里是解决问题的 JS。 我只是检查某些功能是否被选中/取消选中并切换它们。
// force enabling those settings
['reducedUI', 'keepCaretInsideBlock', 'showBlockBreadcrumbs', 'focusMode', 'themeStyles'].forEach(mustBeEnabled => {
if (!wp.data.select('core/edit-post').isFeatureActive(mustBeEnabled)) {
wp.data.dispatch('core/edit-post').toggleFeature(mustBeEnabled);
}
});
// force disabling those settings
['fullscreenMode', 'fixedToolbar', 'welcomeGuide', 'showIconLabels', 'mostUsedBlocks'].forEach(mustBeDisabled => {
if (wp.data.select('core/edit-post').isFeatureActive(mustBeDisabled)) {
wp.data.dispatch('core/edit-post').toggleFeature(mustBeDisabled);
}
});
这个JSX文件已经用@wordpress/scripts编译了
【讨论】:
npm init 以创建package.json 文件,然后您可以按照@wordpress/scripts 中的小文档进行操作