wppaste
WordPress

wp_kses_hair( string $attr, string[] $allowed_protocols ): array[]

Since
1.0.0
Source
wp-includes/kses.php:1391
Builds an attribute list from string containing attributes.

Description

This function does a lot of work. It parses an attribute list into an array with attribute data, and tries to do the right thing even if it gets weird input. It will add quotes around attribute values that don't have any quotes or apostrophes around them, to make it easier to produce HTML code that will conform to W3C's HTML specification. It will also remove bad URL protocols from attribute values. It also reduces duplicate attributes by using the attribute defined first (foo='bar' foo='baz' will result in foo='bar').

Compatibility

WordPress
since 1.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[]
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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
15–24

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 191.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always15–24wp_kses_uri_attributes()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev19115–2420
8.519115–2420
8.419115–242033 fewer instructions than PHP 8.3
8.322415–2420
8.222415–24201 more instruction than PHP 8.1
8.122315–2420
7.422315–2420

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

Used by · 4

Source code

function wp_kses_hair( $attr, $allowed_protocols ) {	$attrarr  = array();	$mode     = 0;	$attrname = '';	$uris     = wp_kses_uri_attributes(); 	// Loop through the whole attribute list. 	while ( strlen( $attr ) !== 0 ) {		$working = 0; // Was the last operation successful? 		switch ( $mode ) {			case 0:				if ( preg_match( '/^([_a-zA-Z][-_a-zA-Z0-9:.]*)/', $attr, $match ) ) {					$attrname = $match[1];					$working  = 1;					$mode     = 1;					$attr     = preg_replace( '/^[_a-zA-Z][-_a-zA-Z0-9:.]*/', '', $attr );				} 				break; 			case 1:				if ( preg_match( '/^\s*=\s*/', $attr ) ) { // Equals sign.					$working = 1;					$mode    = 2;					$attr    = preg_replace( '/^\s*=\s*/', '', $attr );					break;				} 				if ( preg_match( '/^\s+/', $attr ) ) { // Valueless.					$working = 1;					$mode    = 0; 					if ( false === array_key_exists( $attrname, $attrarr ) ) {						$attrarr[ $attrname ] = array(							'name'  => $attrname,							'value' => '',							'whole' => $attrname,							'vless' => 'y',						);					} 					$attr = preg_replace( '/^\s+/', '', $attr );				} 				break; 			case 2:				if ( preg_match( '%^"([^"]*)"(\s+|/?$)%', $attr, $match ) ) {					// "value"					$thisval = $match[1];					if ( in_array( strtolower( $attrname ), $uris, true ) ) {						$thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );					} 					if ( false === array_key_exists( $attrname, $attrarr ) ) {						$attrarr[ $attrname ] = array(							'name'  => $attrname,							'value' => $thisval,							'whole' => "$attrname=\"$thisval\"",							'vless' => 'n',						);					} 					$working = 1;					$mode    = 0;					$attr    = preg_replace( '/^"[^"]*"(\s+|$)/', '', $attr );					break;				} 				if ( preg_match( "%^'([^']*)'(\s+|/?$)%", $attr, $match ) ) {					// 'value'					$thisval = $match[1];					if ( in_array( strtolower( $attrname ), $uris, true ) ) {						$thisval = wp_kses_bad_protocol( $thisval, $allowed_protocols );					} 					if ( false === array_key_exists( $attrname, $attrarr ) ) {						$attrarr[ $attrname ] = array(

Changelog

Introduced in 1.0.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

7.0.4
Return type changed from array[] to array<string,.verified against source
1.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 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.