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:382
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
- Scaling
- Scales with input
- Instructions
- 9–126
- 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 198.
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 · 13 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 | 9–21 | none |
isset($value) && !tag_name() | 26–32 | tag_name() |
isset($value) && tag_name() | 36–42 | tag_name(), ->set_attribute(), ->get_updated_html() |
isset($value) | 39–44 | explode(), ->next_tag(), ->set_bookmark(), ->release_bookmark() |
isset($value) | 49–55 | explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), ->get_attribute_names_with_prefix(), ->release_bookmark() |
isset($value) && !$selectors | 80–89 | explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), strcasecmp(), ->release_bookmark(), ->get_attribute_names_with_prefix(), wp_kses_post(), ->next_tag(), ->next_tag(), ->get_updated_html() |
isset($value) && !$selectors && tag_name() | 85–94 | explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), strcasecmp(), tag_name(), ->release_bookmark(), ->get_attribute_names_with_prefix(), wp_kses_post(), ->next_tag(), ->next_tag(), ->get_updated_html() |
isset($value) && !$selectors | 90–100 | explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), ->get_attribute_names_with_prefix(), ->get_tag(), strcasecmp(), ->release_bookmark(), ->get_attribute_names_with_prefix(), wp_kses_post(), ->next_tag(), ->next_tag(), ->get_updated_html() |
isset($value) && !$selectors && tag_name() | 95–105 | explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), ->get_attribute_names_with_prefix(), ->get_tag(), strcasecmp(), tag_name(), ->release_bookmark(), ->get_attribute_names_with_prefix(), wp_kses_post(), ->next_tag(), ->next_tag(), ->get_updated_html() |
isset($value) && !$selectors | 103–110 | explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), strcasecmp(), ->release_bookmark(), ->get_attribute_names_with_prefix(), wp_kses_post(), ->next_tag(), ->next_tag(), ->get_updated_html(), ->next_tag(), ->get_updated_html() |
isset($value) && !$selectors && tag_name() | 108–115 | explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), strcasecmp(), tag_name(), ->release_bookmark(), ->get_attribute_names_with_prefix(), wp_kses_post(), ->next_tag(), ->next_tag(), ->get_updated_html(), ->next_tag(), ->get_updated_html() |
isset($value) && !$selectors | 113–121 | explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), ->get_attribute_names_with_prefix(), ->get_tag(), strcasecmp(), ->release_bookmark(), ->get_attribute_names_with_prefix(), wp_kses_post(), ->next_tag(), ->next_tag(), ->get_updated_html(), ->next_tag(), ->get_updated_html() |
1 further outcome, up to 126 instructions
isset($value) && !$selectors && tag_name() | 118–126 | explode(), ->next_tag(), ->set_bookmark(), ->get_tag(), ->get_attribute_names_with_prefix(), ->get_tag(), strcasecmp(), tag_name(), ->release_bookmark(), ->get_attribute_names_with_prefix(), wp_kses_post(), ->next_tag(), ->next_tag(), ->get_updated_html(), ->next_tag(), ->get_updated_html() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 198 | 9–126 | 22 | |
| 8.5 | 198 | 9–126 | 22 | |
| 8.4 | 198 | 9–126 | 22 | |
| 8.3 | 198 | 9–126 | 22 | |
| 8.2 | 198 | 9–126 | 22 | 1 more instruction than PHP 8.1 |
| 8.1 | 197 | 9–128 | 22 | |
| 7.4 | 197 | 9–128 | 22 |
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.
Uses · 2
- wp_kses_post()Sanitizes content for allowed HTML tags for post content.
- WP_HTML_Tag_Processor::__construct()Constructor.
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 ) { $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 = new WP_HTML_Tag_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' ); // TODO: This shouldn't be needed when the `set_inner_html` function is ready. // Store the parent tag and its attributes to be able to restore them later in the button. // The button block has a wrapper while the paragraph and heading blocks don't. if ( 'core/button' === $this->name ) { $button_wrapper = $block_reader->get_tag(); $button_wrapper_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); $button_wrapper_attrs = array(); foreach ( $button_wrapper_attribute_names as $name ) { $button_wrapper_attrs[ $name ] = $block_reader->get_attribute( $name ); } } 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, ) ) ) { $block_reader->release_bookmark( 'iterate-selectors' ); // TODO: Use `set_inner_html` method whenever it's ready in the HTML API. // Until then, it is hardcoded for the paragraph, heading, and button blocks. // Store the tag and its attributes to be able to restore them later. $selector_attribute_names = $block_reader->get_attribute_names_with_prefix( '' ); $selector_attrs = array(); foreach ( $selector_attribute_names as $name ) { $selector_attrs[ $name ] = $block_reader->get_attribute( $name ); } $selector_markup = "<$selector>" . wp_kses_post( $source_value ) . "</$selector>"; $amended_content = new WP_HTML_Tag_Processor( $selector_markup ); $amended_content->next_tag(); foreach ( $selector_attrs as $attribute_key => $attribute_value ) { $amended_content->set_attribute( $attribute_key, $attribute_value ); } if ( 'core/paragraph' === $this->name || 'core/heading' === $this->name ) { return $amended_content->get_updated_html(); } if ( 'core/button' === $this->name ) { $button_markup = "<$button_wrapper>{$amended_content->get_updated_html()}</$button_wrapper>"; $amended_button = new WP_HTML_Tag_Processor( $button_markup ); $amended_button->next_tag(); foreach ( $button_wrapper_attrs as $attribute_key => $attribute_value ) { $amended_button->set_attribute( $attribute_key, $attribute_value ); } return $amended_button->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'], )Changelog
Introduced in 6.5.0. One change between 6.7.10 and 7.1.3.
Signature, return type and hooks compared across 5 parsed releases.
$inner_block_offsets added.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.11 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.