WP_Block_Processor::get_block_type(): string|null

Allocates a substring for the block type and returns the fully-qualified name, including the namespace, if matched on a delimiter, otherwise null.

Description

This function is like WP_Block_Processor::get_printable_block_type() but when paused on a freeform HTML block, will return null instead of “core/freeform”.
The null behavior matches what parse_blocks() returns but may not be as useful as having a string value.

This function allocates a substring for the given block type. This allocation will be small and likely fine in most cases, but it’s preferable to call WP_Block_Processor::is_block_type() if only needing to know whether the delimiter is for a given block type, as that function is more efficient for this purpose and avoids the allocation.

Example:

// Avoid.
'core/paragraph' = $processor->get_block_type();

// Prefer.
$processor->is_block_type( 'core/paragraph' );
$processor->is_block_type( 'paragraph' );
$processor->is_block_type( 'core/freeform' );

// Freeform HTML content has no block type.
$processor = new WP_Block_Processor( 'non-block content' );
$processor->next_token();
null === $processor->get_block_type();

See also

Return

string|null Fully-qualified block namespace and type, e.g. "core/paragraph", if matched on an explicit delimiter, otherwise null.

Source

public function get_block_type(): ?string {
	if (
		self::READY === $this->state ||
		self::COMPLETE === $this->state ||
		self::INCOMPLETE_INPUT === $this->state
	) {
		return null;
	}

	// This is a core/freeform text block, it’s special.
	if ( $this->is_html() ) {
		return null;
	}

	$block_type = substr( $this->source_text, $this->namespace_at, $this->name_at - $this->namespace_at + $this->name_length );
	return self::normalize_block_type( $block_type );
}

Changelog

VersionDescription
6.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.