WP_Block_Styles_Registry::register() WordPress Method

The WP_Block_Styles_Registry::register() method is used to register a new block style. Block styles are similar to post styles and allow you to specify different styling for blocks.

WP_Block_Styles_Registry::register( string $block_name, array $style_properties ) #

Registers a block style for the given block type.


Parameters

$block_name

(string)(Required)Block type name including namespace.

$style_properties

(array)(Required)Array containing the properties of the style name, label, is_default, style_handle (name of the stylesheet to be enqueued), inline_style (string containing the CSS to be added).


Top ↑

Return

(bool) True if the block style was registered with success and false otherwise.


Top ↑

Source

File: wp-includes/class-wp-block-styles-registry.php

	public function register( $block_name, $style_properties ) {

		if ( ! isset( $block_name ) || ! is_string( $block_name ) ) {
			_doing_it_wrong(
				__METHOD__,
				__( 'Block name must be a string.' ),
				'5.3.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'];

		if ( ! isset( $this->registered_block_styles[ $block_name ] ) ) {
			$this->registered_block_styles[ $block_name ] = array();
		}
		$this->registered_block_styles[ $block_name ][ $block_style_name ] = $style_properties;

		return true;
	}


Top ↑

Changelog

Changelog
VersionDescription
5.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.