函数文档

unregister_block_style()

💡 云策文档标注

概述

unregister_block_style() 函数用于取消注册块样式,仅适用于通过服务器端 register_block_style 注册的样式。该函数返回布尔值表示操作是否成功。

关键要点

  • 函数参数:$block_name(块类型名称,包括命名空间)和 $block_style_name(块样式名称),均为必需字符串。
  • 返回值:成功取消注册返回 true,否则返回 false。
  • 仅能取消注册服务器端注册的样式,无法处理客户端注册的样式。
  • 内部实现调用 WP_Block_Styles_Registry::get_instance()->unregister() 方法。
  • 自 WordPress 5.3.0 版本引入。

代码示例

unregister_block_style( 'core/quote', 'fancy-quote' );

注意事项

  • unregister_block_style() 不能取消注册通过客户端代码(如 JavaScript)注册的样式,需使用 wp.blocks.unregisterBlockStyle()。
  • 对于核心块默认样式(如 core/quote 的 'plain' 样式),直接使用 unregister_block_style() 可能无效,可尝试过滤 register_block_type_args 来移除样式。

📄 原文内容

Unregisters a block style.

Parameters

$block_namestringrequired
Block type name including namespace.
$block_style_namestringrequired
Block style name.

Return

bool True if the block style was unregistered with success and false otherwise.

Source

function unregister_block_style( $block_name, $block_style_name ) {
	return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name );
}

Changelog

Version Description
5.3.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    The following code sample unregister the style named ‘fancy-quote’ from the core quote block:

    unregister_block_style( 'core/quote', 'fancy-quote' );

    Important: The function unregister_block_style only unregisters styles that were registered on the server using register_block_style. The function does not unregister a style registered using client-side code.

  2. Skip to note 5 content

    As Marie Comet mentioned, you cannot use unregister_block_style() on a style that was registered serverside (e.g. via PHP). The following PHP will not unregister a block style from the core/table block:

    // PHP
    unregister_block_style( 'core/table', 'stripes' );

    You can use the Block API via JavaScript to unregister it. Running the following JavaScript will successfully unregister the style.

    // JavaScript
    wp.blocks.unregisterBlockStyle( 'core/table', 'stripes' );

    For more info, look under Styles in the Block API Reference of the Block Editor Handbook.

  3. Skip to note 6 content

    I wanted to unregister default core/quote block Plain style.

    First I have tried unregister_block_style(‘core/quote’, ‘plain’) – not working!

    Then I tried JS wp.blocks.unregisterBlockStyle() – partly working! Block style was removed from editor, but was present in FSE Style guide section!…

    Then I came back to PHP and tried to filter block.json args:

    add_filter( 'register_block_type_args', function ( $args, $block_type ) {
    	if ( 'core/quote' === $block_type ) {
    		unset($args['styles']);
    	}
    
    	return $args;
    }, 10, 2 );

    … And it worked finally! This API seems inconsistent… (