wp_kses_hair( string $attr, string[] $allowed_protocols ): array<string,
- Since
- 1.0.0, 7.0.0
- Source
wp-includes/kses.php:1623
Description
This function performs a number of transformations while parsing attribute strings:
- It normalizes attribute values and surrounds them with double quotes.
- It normalizes HTML character references inside attribute values.
- It removes “bad” URL protocols from attribute values.
Otherwise this reads the attributes as if they were part of an HTML tag. It performs these transformations to lower the risk of mis-parsing down the line and to perform URL sanitization in line with the rest of the kses subsystem. Importantly, it does not decode the attribute values, meaning that special HTML syntax characters will be left with character references in the value property.
Example:
$attrs = wp_kses_hair( 'class="is-wide" inert data-lazy=\'<img>\' =/🐮=/' );
$attrs === array(
'class' => array( 'name' => 'class', 'value' => 'is-wide', 'whole' => 'class="is-wide"', 'vless' => 'n' ),
'inert' => array( 'name' => 'inert', 'value' => '', 'whole' => 'inert', 'vless' => 'y' ),
'data-lazy' => array( 'name' => 'data-lazy', 'value' => '<img>', 'whole' => 'data-lazy="<img>"', 'vless' => 'n' ),
'=' => array( 'name' => '=', 'value' => '', 'whole' => '=', 'vless' => 'y' ),
'🐮' => array( 'name' => '🐮', 'value' => '/', 'whole' => '🐮="/"', 'vless' => 'n' ),
);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 every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$attrstring- Attribute list from HTML element to closing HTML element tag.
$allowed_protocolsstring[]- Array of allowed URL protocols.
Return value
array<string,- array{name: string, value: string, whole: string, vless: 'y'|'n'}> Array of attribute information after parsing.
Performance profile
How much work a call to wp_kses_hair() 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
- 22–28
- Plugin surface
- None
- Called by
- 4
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 68.
Nothing here hands control to plugin code.
4 places 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_hair()
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_kses_hair() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 22–28 | wp_kses_uri_attributes(), ->next_token(), ->get_attribute_names_with_prefix() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 68 | 22–28 | 9 | |
| 8.5 | 68 | 22–28 | 9 | |
| 8.4 | 68 | 22–28 | 9 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 75 | 22–28 | 9 | |
| 8.2 | 75 | 22–28 | 9 | |
| 8.1 | 75 | 22–28 | 9 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 76 | 22–28 | 9 |
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
- wp_kses_uri_attributes()Returns an array of HTML attribute names whose value contains a URL.
- wp_kses_bad_protocol()Sanitizes a string and removed disallowed URL protocols.
- WP_HTML_Tag_Processor::__construct()Constructor.
Used by · 4
- wp_filter_oembed_iframe_title_attribute()Filters the given oEmbed HTML to make sure iframes have a title attribute.
- wp_kses_attr()Removes all attributes, if none are allowed for this element.
- wp_rel_callback()Callback to add a rel attribute to HTML A element.
- wp_targeted_link_rel_callback()Callback to add `rel="noopener"` string to HTML A element.
Source code
function wp_kses_hair( $attr, $allowed_protocols ) { $attributes = array(); $uris = wp_kses_uri_attributes(); $processor = new WP_HTML_Tag_Processor( "<wp {$attr}>" ); $processor->next_token(); $attribute_names = $processor->get_attribute_names_with_prefix( '' ); if ( null === $attribute_names || 0 === count( $attribute_names ) ) { return $attributes; } $syntax_characters = array( '&' => '&', '<' => '<', '>' => '>', "'" => ''', '"' => '"', ); foreach ( $attribute_names as $name ) { $value = $processor->get_attribute( $name ); $is_bool = true === $value; if ( is_string( $value ) && in_array( $name, $uris, true ) ) { $value = wp_kses_bad_protocol( $value, $allowed_protocols ); } // Reconstruct and normalize the attribute value. $recoded = $is_bool ? '' : strtr( $value, $syntax_characters ); $whole = $is_bool ? $name : "{$name}=\"{$recoded}\""; $attributes[ $name ] = array( 'name' => $name, 'value' => $recoded, 'whole' => $whole, 'vless' => $is_bool ? 'y' : 'n', ); } return $attributes;}Changelog
Introduced in 1.0.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
array[] to array<string,.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.