wp_kses_split2( string $content, array[]|string $allowed_html, string[] $allowed_protocols ): string
- Since
- 1.0.0, 6.6.0
- Source
wp-includes/kses.php:1396
wp_kses_split() for fixing malformed HTML tags.Description
This function does a lot of work. It rejects some very malformed things like <:::>. It returns an empty string, if the element isn't allowed (look ma, no strip_tags()!). Otherwise it splits the tag into an element and an attribute list.
After the tag is split into an element and an attribute list, it is run through another filter which will remove illegal attributes and once that is completed, will be returned.
Compatibility
- WordPress
- since 6.6.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
$contentstring- Content to filter.
$allowed_htmlarray[]|string- An array of allowed HTML elements and attributes, or a context name such as 'post'. See wp_kses_allowed_html() for the list of accepted context names.
$allowed_protocolsstring[]- Array of allowed URL protocols.
Return value
string- Fixed HTML element
Performance profile
How much work a call to wp_kses_split2() 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–44
- 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 95.
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 it touches
- hookthird-party callbacks
apply_filters()one call below wp_kses_split2()
Further down the call graph this can also reach option, cache, serialize, query 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 · 7 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_kses_split2() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$content | 10 | wp_kses_stripslashes() |
!preg_match() | 21 | wp_kses_stripslashes(), preg_match() |
$content | 29–38 | wp_kses_stripslashes(), wp_kses() |
preg_match() && is_array($allowed_html) | 32–37 | wp_kses_stripslashes(), preg_match(), strtolower() |
preg_match() && !is_array($allowed_html) | 36–41 | wp_kses_stripslashes(), preg_match(), wp_kses_allowed_html(), strtolower() |
preg_match() && is_array($allowed_html) && isset($allowed_html) && $slash === "" | 40 | wp_kses_stripslashes(), preg_match(), strtolower(), wp_kses_attr() |
preg_match() && !is_array($allowed_html) && isset($allowed_html) && $slash === "" | 44 | wp_kses_stripslashes(), preg_match(), wp_kses_allowed_html(), strtolower(), wp_kses_attr() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 95 | 10–44 | 10 | |
| 8.5 | 95 | 10–44 | 10 | |
| 8.4 | 95 | 10–44 | 10 | 24 fewer instructions than PHP 8.3 |
| 8.3 | 119 | 13–56 | 10 | |
| 8.2 | 119 | 13–56 | 10 | |
| 8.1 | 119 | 13–56 | 10 | |
| 7.4 | 119 | 13–56 | 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.
Uses · 5
- wp_kses_stripslashes()Strips slashes from in front of quotes.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_kses()Filters text content and strips out disallowed HTML.
- wp_kses_allowed_html()Returns an array of allowed HTML tags and attributes for a given context.
- wp_kses_attr()Removes all attributes, if none are allowed for this element.
Used by · 1
- _wp_kses_split_callback()Callback for `wp_kses_split()`.
Source code
function wp_kses_split2( $content, $allowed_html, $allowed_protocols ) { $content = wp_kses_stripslashes( $content ); /* * The regex pattern used to split HTML into chunks attempts * to split on HTML token boundaries. This function should * thus receive chunks that _either_ start with meaningful * syntax tokens, like a tag `<div>` or a comment `<!-- ... -->`. * * If the first character of the `$content` chunk _isn't_ one * of these syntax elements, which always starts with `<`, then * the match had to be for the final alternation of `>`. In such * case, it's probably standing on its own and could be encoded * with a character reference to remove ambiguity. * * In other words, if this chunk isn't from a match of a syntax * token, it's just a plaintext greater-than (`>`) sign. */ if ( ! str_starts_with( $content, '<' ) ) { return '>'; } /* * When certain invalid syntax constructs appear, the HTML parser * shifts into what's called the "bogus comment state." This is a * plaintext state that consumes everything until the nearest `>` * and then transforms the entire span into an HTML comment. * * Preserve these comments and do not treat them like tags. * * @see https://html.spec.whatwg.org/#bogus-comment-state */ if ( 1 === preg_match( '~^(?:</[^a-zA-Z][^>]*>|<![a-z][^>]*>)$~', $content ) ) { /** * Since the pattern matches `</…>` and also `<!…>`, this will * preserve the type of the cleaned-up token in the output. */ $opener = $content[1]; $content = substr( $content, 2, -1 ); do { $prev = $content; $content = wp_kses( $content, $allowed_html, $allowed_protocols ); } while ( $prev !== $content ); // Recombine the modified inner content with the original token structure. return "<{$opener}{$content}>"; } /* * Normative HTML comments should be handled separately as their * parsing rules differ from those for tags and text nodes. */ if ( str_starts_with( $content, '<!--' ) ) { $content = str_replace( array( '<!--', '-->' ), '', $content ); while ( ( $newstring = wp_kses( $content, $allowed_html, $allowed_protocols ) ) !== $content ) { $content = $newstring; } if ( '' === $content ) { return ''; } // Prevent multiple dashes in comments. $content = preg_replace( '/--+/', '-', $content ); // Prevent three dashes closing a comment. $content = preg_replace( '/-$/', '', $content ); return "<!--{$content}-->"; } // It's seriously malformed. if ( ! preg_match( '%^<\s*(/\s*)?([a-zA-Z0-9-]+)([^>]*)>?$%', $content, $matches ) ) { return ''; } $slash = trim( $matches[1] ); $elem = $matches[2]; $attrlist = $matches[3];Changelog
Introduced in 1.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/kses.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.