【问题标题】:Toggle Gutenberg preferences by user role按用户角色切换 Gutenberg 偏好
【发布时间】:2021-04-27 15:58:35
【问题描述】:

我正在寻找一种方法来根据用户角色在 Gutenberg 编辑器侧边栏中隐藏一些页面设置面板。由于这些面板标题没有特定的类,我无法使用 CSS 来定位它们,所以我想知道是否有办法至少控制哪些面板在首选项中处于活动状态:

有谁知道我是否可以使用挂钩或其他东西来根据用户角色有条件地预设这些切换?

非常感谢。

【问题讨论】:

    标签: wordpress wordpress-gutenberg wordpress-hook


    【解决方案1】:

    我在 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编译了

    【讨论】:

    • 啊,有趣的方法,谢谢!我现在有一些不同的项目,我对 JSX 或编译器脚本不太熟悉,但我仍然对解决方案感兴趣,所以我会在有时间的时候尝试测试一下。
    • 要设置“节点项目”,您只需要在您的计算机上安装节点。运行npm init 以创建package.json 文件,然后您可以按照@wordpress/scripts 中的小文档进行操作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-12
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多