从 WordPress 核心古腾堡块中删除属性

发布于 2025-01-17 05:19:51 字数 841 浏览 0 评论 0原文

随着新的 WordPress 更新,核心 Gutenberg 块中添加了一些新属性,以便某些用户更轻松地自定义这些块

new quote block

在上图中,您可以看到新外观的示例引用块的。例如,现在可以选择更改版式的外观以及新的“简单”样式选项。我想删除这些选项,但到目前为止我发现的唯一一件事是如何添加额外的属性以及如何完全删除块。

对于样式,我找到了获取属性的钩子,但我不知道如何修改它,因此它不显示“普通”选项:

    // hide "plain" opiton inside the style tab of the quote block.
const {
    addFilter,
} = wp.hooks;
addFilter(
    'blocks.registerBlockType',
    'jsforwp-advgb/extend-quote-block',
    extendBlockQuoteBlock
);

function extendBlockQuoteBlock( settings, name ) {
    if ( !settings || !settings.supports ) {
        return settings;
    }
    if ( 'core/quote' !== name ) return settings;
    console.log( settings.styles );

    return settings;
}

With the new wordpress update there are some new properties that have been added to the core gutenberg blocks to make it easier for certain users to customize these blocks

new quote block

In the image above you can see the example for the new looks of the quote block. For instance there is now the option to change the appearance of the typography as well as a new 'plain' option for the styles. I want to remove these options, but The only thing i have found so far is how to add extra properties nd how to remove the block entierly.

For the styles, i have found the hook to get the property, but I don't know how i can modify this so it doesn't show the "plain" option:

    // hide "plain" opiton inside the style tab of the quote block.
const {
    addFilter,
} = wp.hooks;
addFilter(
    'blocks.registerBlockType',
    'jsforwp-advgb/extend-quote-block',
    extendBlockQuoteBlock
);

function extendBlockQuoteBlock( settings, name ) {
    if ( !settings || !settings.supports ) {
        return settings;
    }
    if ( 'core/quote' !== name ) return settings;
    console.log( settings.styles );

    return settings;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2025-01-24 05:19:51

可以使用 unregisterBlockStyle(blockName, styleVariationName) 删除块样式。无需扩展或添加过滤器到块注册,只需在 DOM 准备好防止竞争条件时使用 unregisterBlockStyle 即可:

wp.domReady( function () {
    wp.blocks.unregisterBlockStyle( 'core/quote', 'plain' );
} );

参考:块编辑器手册/参考指南/块 API 参考/样式

更新: 下面是截图显示在控制台中运行 wp.blocks.unregisterBlockStyle( 'core/quote', 'plain' ) 的效果。
unregisterBlockStyle

如果您希望删除排版,它与块样式不同,并且来自块支持 API,这是通过 addFilter 和 lodash allocate 完成的,以删除对您不想要的功能的支持,例如:

wp.hooks.addFilter(
    'blocks.registerBlockType',
    'jsforwp-advgb/extend-quote-block',
    extendBlockQuoteBlock
);

function extendBlockQuoteBlock(settings, name) {
    if (name !== 'core/quote') {
        return settings;
    }

    return lodash.assign({}, settings, {
        supports: lodash.assign({}, settings.supports, {
            typography: false,
        }),
    });
}

Block styles can be removed with unregisterBlockStyle(blockName, styleVariationName). There is no need extend or add a filter to the block registration, just use unregisterBlockStyle when the DOM is ready to prevent a race condition:

wp.domReady( function () {
    wp.blocks.unregisterBlockStyle( 'core/quote', 'plain' );
} );

Ref: Block Editor Handbook / Reference Guides / Block API Reference / Styles

Update: Below is a screenshot showing the effect of running wp.blocks.unregisterBlockStyle( 'core/quote', 'plain' ) in the console.
unregisterBlockStyle

If you wish to remove typography, it is different to block styles and is from the block supports API, which is done with addFilter and lodash assign to remove support for the features you dont want, eg:

wp.hooks.addFilter(
    'blocks.registerBlockType',
    'jsforwp-advgb/extend-quote-block',
    extendBlockQuoteBlock
);

function extendBlockQuoteBlock(settings, name) {
    if (name !== 'core/quote') {
        return settings;
    }

    return lodash.assign({}, settings, {
        supports: lodash.assign({}, settings.supports, {
            typography: false,
        }),
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文