WP_Block::replace_html( string $block_content, string $attribute_name, mixed $source_value, int[] $inner_block_offsets = array() ): string
- Since
- 6.5.0, 7.1.0
- Source
wp-includes/class-wp-block.php:376
Compatibility
- WordPress
- since 7.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), 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.
$inner_block_offsetsint[]optional- Byte offsets where each inner block's rendered output begins. Default empty array.Default:
array()
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
- Scaling
- Scales with input
- Instructions
- 10–64
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 107.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10–22 | none |
isset($value) && !tag_name() | 27–33 | ->next_tag(), tag_name() |
isset($value) && tag_name() | 37–43 | ->next_tag(), tag_name(), ->set_attribute(), ->get_updated_html() |
isset($value) | 37–42 | ::get_block_bindings_processor(), explode(), ->next_tag(), ->set_bookmark(), ->release_bookmark() |
isset($value) && !$selectors | 55–59 | ::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() | 60–64 | ::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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 107 | 10–64 | 10 | |
| 8.5 | 107 | 10–64 | 10 | |
| 8.4 | 107 | 10–64 | 10 | |
| 8.3 | 107 | 10–64 | 10 | |
| 8.2 | 107 | 10–64 | 10 | 1 more instruction than PHP 8.1 |
| 8.1 | 106 | 10–66 | 10 | |
| 7.4 | 106 | 10–66 | 10 |
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
- WP_Block::render()Generates the render output for the block.
Source code
private function replace_html( string $block_content, string $attribute_name, $source_value, array $inner_block_offsets = array() ) { $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()` once it's available. * Any replacement must preserve already-rendered inner block * markup verbatim (it may come from dynamic blocks), so it * cannot re-serialize the element's contents. Until an API with * that guarantee exists, the replacement is spliced by byte * offset, leaving inner block output untouched. */ $block_reader->release_bookmark( 'iterate-selectors' ); $block_reader->replace_rich_text( wp_kses_post( $source_value ), $inner_block_offsets ); 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.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$inner_block_offsets added.verified against source$inner_block_offsets parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.