如何更改 WP Gutenberg 的类别组件面板?

How to change WP Gutenberg's category components panel?

在 Gutenberg Editor 中,我尝试修改类别面板(在右侧我选择 post 将放入的类别的面板)。 如果该类别具有子类别,则不应向该类别添加 post。由于类别是静态的,所以使用category-id就可以了。

我的想法是使用 enqueue_block_editor_assets 并添加一些 javascript 以通过元素的 ID 禁用复选框。 这不起作用,找不到该元素:-(

到目前为止,这是我无法运行的代码:

functions.php:

function gutenberg_enqueue()
{
    wp_enqueue_script(
        'gutenberg-additions-script',
        get_stylesheet_directory_uri().'/gutenberg-additions.js',
        array(), true, true
    );
}
add_action('enqueue_block_editor_assets', 'gutenberg_enqueue', 999);

(我用的是get_stylesheet_directory_uri(),因为我是儿童主题)

古腾堡-additions.js:

window.onload = function () {
    var cat1 = document.getElementById('editor-post-taxonomies-hierarchical-term-1');
    if (cat1 != null) {
        cat1.disabled = true;
    }

欢迎使用 Whosebug。 gutenberg github directory 中有一个例子。它是用旧的 es5 语法编写的,但应该很容易转移到 esnext。它使用 editor.PostTaxonomyType 过滤器来编辑分类组件。

var el = wp.element.createElement;

function customizeProductTypeSelector( OriginalComponent ) {
    return function( props ) {
        if ( props.slug === 'product-type' ) {
            return el(
                'div',
                {},
                'Product Type Selector'
            );
        } else {
            return el(
                OriginalComponent,
                props
            );
        }
    }
};

wp.hooks.addFilter(
    'editor.PostTaxonomyType',
    'my-custom-plugin',
    customizeProductTypeSelector
);

如果您需要更多信息,另请阅读 this github issue 上的评论。