WP_Block_Styles_Registry::registerpublicWP 5.3.0

Registers a block style for the given block type.

If the block styles are present in a standalone stylesheet, register it and pass its handle as the style_handle argument. If the block styles should be inline, use the inline_style argument. Usually, one of them would be used to pass CSS styles. However, you could also skip them and provide CSS styles in any stylesheet or with an inline tag.

Метод класса: WP_Block_Styles_Registry{}

Хуков нет.

Возвращает

true|false. True if the block style was registered with success and false otherwise.

Использование

$WP_Block_Styles_Registry = new WP_Block_Styles_Registry();
$WP_Block_Styles_Registry->register( $block_name, $style_properties );
$block_name(строка|string[]) (обязательный)
Block type name including namespace or array of namespaced block type names.
$style_properties(массив) (обязательный)

Array containing the properties of the style.

  • name(строка)
    The identifier of the style used to compute a CSS class.

  • label(строка)
    A human-readable label for the style.

  • inline_style(строка)
    Inline CSS code that registers the CSS class required for the style.

  • style_handle(строка)
    The handle to an already registered style that should be enqueued in places where block styles are needed.

  • is_default(true|false)
    Whether this is the default style for the block type.

  • style_data(массив)
    Theme.json-like object to generate CSS from.

Список изменений

С версии 5.3.0 Введена.
С версии 6.6.0 Added ability to register style across multiple block types along with theme.json-like style data.

Код WP_Block_Styles_Registry::register() WP 6.8.1

public function register( $block_name, $style_properties ) {

	if ( ! is_string( $block_name ) && ! is_array( $block_name ) ) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Block name must be a string or array.' ),
			'6.6.0'
		);
		return false;
	}

	if ( ! isset( $style_properties['name'] ) || ! is_string( $style_properties['name'] ) ) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Block style name must be a string.' ),
			'5.3.0'
		);
		return false;
	}

	if ( str_contains( $style_properties['name'], ' ' ) ) {
		_doing_it_wrong(
			__METHOD__,
			__( 'Block style name must not contain any spaces.' ),
			'5.9.0'
		);
		return false;
	}

	$block_style_name = $style_properties['name'];
	$block_names      = is_string( $block_name ) ? array( $block_name ) : $block_name;

	// Ensure there is a label defined.
	if ( empty( $style_properties['label'] ) ) {
		$style_properties['label'] = $block_style_name;
	}

	foreach ( $block_names as $name ) {
		if ( ! isset( $this->registered_block_styles[ $name ] ) ) {
			$this->registered_block_styles[ $name ] = array();
		}
		$this->registered_block_styles[ $name ][ $block_style_name ] = $style_properties;
	}

	return true;
}