wppaste
WordPress

WP_Block::replace_html( string $block_content, string $attribute_name, mixed $source_value ): string

Since
6.5.0
Source
wp-includes/class-wp-block.php:373
Depending on the block attribute name, replace its value in the HTML based on the value provided.

Compatibility

WordPress
since 6.5.0
PHP
7.4–8.6-dev
  • 6.7.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$block_contentstring
Block content.
$attribute_namestring
The attribute name to replace.
$source_valuemixed
The value used to replace in the HTML.

Return value

string
The modified block content.

Performance profile

How much work a call to WP_Block::replace_html() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
9–62

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 105.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Block::replace_html() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always9–21none
isset($value) && !tag_name()26–32->next_tag(), tag_name()
isset($value) && tag_name()36–42->next_tag(), tag_name(), ->set_attribute(), ->get_updated_html()
isset($value)36–41::get_block_bindings_processor(), explode(), ->next_tag(), ->set_bookmark(), ->release_bookmark()
isset($value) && !$selectors53–57::get_block_bindings_processor(), explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), strcasecmp(), ->release_bookmark(), wp_kses_post(), ->replace_rich_text(), ->get_updated_html()
isset($value) && !$selectors && tag_name()58–62::get_block_bindings_processor(), explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), strcasecmp(), tag_name(), ->release_bookmark(), wp_kses_post(), ->replace_rich_text(), ->get_updated_html()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1059–6210
8.51059–6210
8.41059–6210
8.31059–6210
8.21059–62101 more instruction than PHP 8.1
8.11049–6410
7.41049–6410

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 1

Source code

	private function replace_html( string $block_content, string $attribute_name, $source_value ) {		$block_type = $this->block_type;		if ( ! isset( $block_type->attributes[ $attribute_name ]['source'] ) ) {			return $block_content;		} 		// Depending on the attribute source, the processing will be different.		switch ( $block_type->attributes[ $attribute_name ]['source'] ) {			case 'html':			case 'rich-text':				$block_reader = self::get_block_bindings_processor( $block_content ); 				// TODO: Support for CSS selectors whenever they are ready in the HTML API.				// In the meantime, support comma-separated selectors by exploding them into an array.				$selectors = explode( ',', $block_type->attributes[ $attribute_name ]['selector'] );				// Add a bookmark to the first tag to be able to iterate over the selectors.				$block_reader->next_tag();				$block_reader->set_bookmark( 'iterate-selectors' ); 				foreach ( $selectors as $selector ) {					// If the parent tag, or any of its children, matches the selector, replace the HTML.					if ( strcasecmp( $block_reader->get_tag(), $selector ) === 0 || $block_reader->next_tag(						array(							'tag_name' => $selector,						)					) ) {						// TODO: Use `WP_HTML_Processor::set_inner_html` method once it's available.						$block_reader->release_bookmark( 'iterate-selectors' );						$block_reader->replace_rich_text( wp_kses_post( $source_value ) );						return $block_reader->get_updated_html();					} else {						$block_reader->seek( 'iterate-selectors' );					}				}				$block_reader->release_bookmark( 'iterate-selectors' );				return $block_content; 			case 'attribute':				$amended_content = new WP_HTML_Tag_Processor( $block_content );				if ( ! $amended_content->next_tag(					array(						// TODO: build the query from CSS selector.						'tag_name' => $block_type->attributes[ $attribute_name ]['selector'],					)				) ) {					return $block_content;				}				$amended_content->set_attribute( $block_type->attributes[ $attribute_name ]['attribute'], $source_value );				return $amended_content->get_updated_html(); 			default:				return $block_content;		}	}

Changelog

Introduced in 6.5.0. One change between 6.7.10 and 7.1.3.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

Signature, return type and hooks compared across 5 parsed releases.

7.1.3
Parameter $inner_block_offsets added.verified against source
6.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.7 tag, from src/wp-includes/class-wp-block.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.