如何禁用Gutenberg自定义区块中的“Transform to”选项
Gutenberg自定义区块禁用Transform to选项方案
Gutenberg自定义区块的「Transform to」转换选项完全可以禁用,以下是两种可行实现方案:
方法1:注册区块时直接配置(推荐)
在调用registerBlockType注册自定义区块时,主动设置transforms配置项即可:
registerBlockType('your-plugin/your-custom-block', { // 其他原有配置项 supports: { // 你已有的禁用可复用区块配置 reusable: false, }, transforms: { // 禁用当前区块转换到其他区块的选项 to: [], // 若同时需要禁止其他区块转换为当前自定义区块,可将from也设为空数组 from: [], }, // 其他原有配置项 })
该方案无需额外添加钩子,性能最优,适合单个或少量自定义区块的配置场景。
方法2:全局过滤转换规则(适合批量处理)
如果需要统一禁用多个自定义区块的转换选项,可通过blocks.getBlockTransforms过滤器实现:
wp.hooks.addFilter( 'blocks.getBlockTransforms', 'your-plugin/disable-custom-block-transforms', ( transforms, direction, blockName ) => { // 替换为你需要禁用转换功能的自定义区块名称列表 const disabledBlockList = [ 'your-plugin/block1', 'your-plugin/block2' ]; if ( disabledBlockList.includes( blockName ) ) { return []; } return transforms; } );
该方案可以覆盖所有第三方插件、主题额外注入的转换规则,适合多区块统一管控的场景。
注意事项
如果完成配置后仍出现转换选项,可优先检查是否有其他扩展额外注入了针对你的自定义区块的转换规则,使用第二种全局过滤方案即可彻底屏蔽这类额外规则。
内容的提问来源于stack exchange,提问作者kanlukasz
相关产品推荐
相关产品推荐

