wp_strip_custom_css_from_blocks( string $content ): string
- Since
- 7.0.0
- Source
wp-includes/block-supports/custom-css.php:195
style.css in attributes) from all blocks in post content.Description
Uses WP_Block_Parser::next_token() to scan block tokens and surgically replace only the attribute JSON that changed — no parse_blocks() + serialize_blocks() round-trip needed.
Compatibility
- WordPress
- since 7.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$contentstring- Post content to filter, expected to be escaped with slashes.
Return value
string- Filtered post content with block custom CSS removed.
Performance profile
How much work a call to wp_strip_custom_css_from_blocks() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 6–43
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_post().
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 117.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_post()one call below wp_strip_custom_css_from_blocks()
Further down the call graph this can also reach hook, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_strip_custom_css_from_blocks() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!has_blocks() | 6 | has_blocks() |
has_blocks() && !$end && empty($replacements) | 25 | has_blocks(), stripslashes() |
has_blocks() && $end && empty($replacements) | 31 | has_blocks(), stripslashes(), ->next_token() |
has_blocks() && !$end && !empty($replacements) | 34–37 | has_blocks(), stripslashes(), addslashes() |
has_blocks() && $end && !empty($replacements) | 40–43 | has_blocks(), stripslashes(), ->next_token(), addslashes() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 117 | 6–43 | 12 | |
| 8.5 | 117 | 6–43 | 12 | |
| 8.4 | 117 | 6–43 | 12 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 126 | 6–46 | 12 | |
| 8.2 | 126 | 6–46 | 12 | |
| 8.1 | 126 | 6–46 | 12 | |
| 7.4 | 126 | 6–46 | 12 |
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 · 3
- has_blocks()Determines whether a post or content string has blocks.
- serialize_block_attributes()Given an array of attributes, returns a string in the serialized attributes format prepared for post content.
- WP_Block_Parser::__construct()
Source code
function wp_strip_custom_css_from_blocks( $content ) { if ( ! has_blocks( $content ) ) { return $content; } $unslashed = stripslashes( $content ); $parser = new WP_Block_Parser(); $parser->document = $unslashed; $parser->offset = 0; $end = strlen( $unslashed ); $replacements = array(); while ( $parser->offset < $end ) { $next_token = $parser->next_token(); if ( 'no-more-tokens' === $next_token[0] ) { break; } list( $token_type, , $attrs, $start_offset, $token_length ) = $next_token; $parser->offset = $start_offset + $token_length; if ( 'block-opener' !== $token_type && 'void-block' !== $token_type ) { continue; } if ( ! isset( $attrs['style']['css'] ) ) { continue; } // Remove css and clean up empty style. unset( $attrs['style']['css'] ); if ( empty( $attrs['style'] ) ) { unset( $attrs['style'] ); } // Locate the JSON portion within the token. $token_string = substr( $unslashed, $start_offset, $token_length ); $json_rel_start = strcspn( $token_string, '{' ); $json_rel_end = strrpos( $token_string, '}' ); $json_start = $start_offset + $json_rel_start; $json_length = $json_rel_end - $json_rel_start + 1; // Re-encode attributes. If attrs is now empty, remove JSON and trailing space. if ( empty( $attrs ) ) { // Remove the trailing space after JSON. $replacements[] = array( $json_start, $json_length + 1, '' ); } else { $replacements[] = array( $json_start, $json_length, serialize_block_attributes( $attrs ) ); } } if ( empty( $replacements ) ) { return $content; } // Build the result by splicing replacements into the original string. $result = ''; $was_at = 0; foreach ( $replacements as $replacement ) { list( $offset, $length, $new_json ) = $replacement; $result .= substr( $unslashed, $was_at, $offset - $was_at ) . $new_json; $was_at = $offset + $length; } if ( $was_at < $end ) { $result .= substr( $unslashed, $was_at ); } return addslashes( $result );}Changelog
Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.
Signature, return type and hooks compared across 2 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/block-supports/custom-css.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.