wp_kses_one_attr( string $attr, string $element ): string
- Since
- 4.2.3
- Source
wp-includes/kses.php:981
Description
This function can escape data in some situations where wp_kses() must strip the whole attribute.
Compatibility
- WordPress
- since 4.2.3
- 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- The 'whole' attribute, including name and value.
$elementstring- The HTML element name to which the attribute belongs.
Return value
string- Filtered attribute.
Performance profile
How much work a call to wp_kses_one_attr() 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
- Trivial
- Scaling
- Constant
- Instructions
- 58–100
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 109.
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
- regexregular expression over the whole input
preg_split()called directly - hookthird-party callbacks
apply_filters()one call below wp_kses_one_attr()
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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_kses_one_attr() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!str_ends_with() | 58–63 | wp_kses_uri_attributes(), wp_kses_allowed_html(), wp_allowed_protocols(), wp_kses_no_null(), preg_match(), preg_match(), preg_split(), str_ends_with() |
| always | 59–61 | wp_kses_uri_attributes(), wp_kses_allowed_html(), wp_allowed_protocols(), wp_kses_no_null(), preg_match(), preg_match(), preg_split(), wp_kses_attr_check() |
$quote !== "\"" && $quote !== "'" && !$uris | 84–87 | wp_kses_uri_attributes(), wp_kses_allowed_html(), wp_allowed_protocols(), wp_kses_no_null(), preg_match(), preg_match(), preg_split(), esc_attr(), strtolower(), wp_kses_attr_check() |
$quote !== "\"" && $quote !== "'" && $uris | 89–92 | wp_kses_uri_attributes(), wp_kses_allowed_html(), wp_allowed_protocols(), wp_kses_no_null(), preg_match(), preg_match(), preg_split(), esc_attr(), strtolower(), wp_kses_bad_protocol(), wp_kses_attr_check() |
str_ends_with() && !$uris | 90–95 | wp_kses_uri_attributes(), wp_kses_allowed_html(), wp_allowed_protocols(), wp_kses_no_null(), preg_match(), preg_match(), preg_split(), str_ends_with(), esc_attr(), strtolower(), wp_kses_attr_check() |
str_ends_with() && $uris | 95–100 | wp_kses_uri_attributes(), wp_kses_allowed_html(), wp_allowed_protocols(), wp_kses_no_null(), preg_match(), preg_match(), preg_split(), str_ends_with(), esc_attr(), strtolower(), wp_kses_bad_protocol(), wp_kses_attr_check() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 109 | 58–100 | 7 | |
| 8.5 | 109 | 58–100 | 7 | |
| 8.4 | 109 | 58–100 | 7 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 121 | 61–109 | 7 | |
| 8.2 | 121 | 61–109 | 7 | |
| 8.1 | 121 | 61–109 | 7 | |
| 7.4 | 121 | 61–109 | 7 |
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 · 8
- wp_kses_uri_attributes()Returns an array of HTML attribute names whose value contains a URL.
- wp_kses_allowed_html()Returns an array of allowed HTML tags and attributes for a given context.
- wp_allowed_protocols()Retrieves a list of protocols to allow in HTML attributes.
- wp_kses_no_null()Removes any invalid control characters in a text string.
- str_ends_with()Polyfill for `str_ends_with()` function added in PHP 8.0.
- esc_attr()Escaping for HTML attributes.
- wp_kses_bad_protocol()Sanitizes a string and removed disallowed URL protocols.
- wp_kses_attr_check()Determines whether an attribute is allowed.
Used by · 1
- do_shortcodes_in_html_tags()Searches only inside HTML elements for shortcodes and process them.
Source code
function wp_kses_one_attr( $attr, $element ) { $uris = wp_kses_uri_attributes(); $allowed_html = wp_kses_allowed_html( 'post' ); $allowed_protocols = wp_allowed_protocols(); $attr = wp_kses_no_null( $attr, array( 'slash_zero' => 'keep' ) ); // Preserve leading and trailing whitespace. $matches = array(); preg_match( '/^\s*/', $attr, $matches ); $lead = $matches[0]; preg_match( '/\s*$/', $attr, $matches ); $trail = $matches[0]; if ( empty( $trail ) ) { $attr = substr( $attr, strlen( $lead ) ); } else { $attr = substr( $attr, strlen( $lead ), -strlen( $trail ) ); } // Parse attribute name and value from input. $split = preg_split( '/\s*=\s*/', $attr, 2 ); $name = $split[0]; if ( count( $split ) === 2 ) { $value = $split[1]; /* * Remove quotes surrounding $value. * Also guarantee correct quoting in $attr for this one attribute. */ if ( '' === $value ) { $quote = ''; } else { $quote = $value[0]; } if ( '"' === $quote || "'" === $quote ) { if ( ! str_ends_with( $value, $quote ) ) { return ''; } $value = substr( $value, 1, -1 ); } else { $quote = '"'; } // Sanitize quotes, angle braces, and entities. $value = esc_attr( $value ); // Sanitize URI values. if ( in_array( strtolower( $name ), $uris, true ) ) { $value = wp_kses_bad_protocol( $value, $allowed_protocols ); } $attr = "$name=$quote$value$quote"; $vless = 'n'; } else { $value = ''; $vless = 'y'; } // Sanitize attribute by name. wp_kses_attr_check( $name, $value, $attr, $vless, $element, $allowed_html ); // Restore whitespace. return $lead . $attr . $trail;}Changelog
Introduced in 4.2.3. 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.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.