wp_kses_bad_protocol( string $content, string[] $allowed_protocols ): string
- Since
- 1.0.0
- Source
wp-includes/kses.php:1977
Description
This function removes all non-allowed protocols from the beginning of the string. It ignores whitespace and the case of the letters, and it does understand HTML entities. It does its work recursively, so it won't be fooled by a string like javascript:javascript:alert(57).
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
$contentstring- Content to filter bad protocols from.
$allowed_protocolsstring[]- Array of allowed URL protocols.
Return value
string- Filtered content.
Performance profile
How much work a call to wp_kses_bad_protocol() 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
- 12–31
- Plugin surface
- None
- Called by
- 6
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 33.
Nothing here hands control to plugin code.
6 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_kses_bad_protocol() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$content && $allowed_protocols | 12–17 | wp_kses_no_null() |
| always | 22–31 | wp_kses_no_null(), wp_kses_bad_protocol_once() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 33 | 12–31 | 7 | |
| 8.5 | 33 | 12–31 | 7 | |
| 8.4 | 33 | 12–31 | 7 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 45 | 18–43 | 7 | |
| 8.2 | 45 | 18–43 | 7 | |
| 8.1 | 45 | 18–43 | 7 | |
| 7.4 | 45 | 18–43 | 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 · 3
- wp_kses_no_null()Removes any invalid control characters in a text string.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_kses_bad_protocol_once()Sanitizes content from bad protocols and other characters.
Used by · 6
- WP_Http::request()Send an HTTP request to a URI.
- esc_url()Checks and cleans a URL.
- safecss_filter_attr()Filters an inline style attribute and removes disallowed rules.
- wp_http_validate_url()Validates a URL as safe for use in the HTTP API.
- wp_kses_hair()Builds an attribute list from string containing attributes.
- wp_kses_one_attr()Filters one HTML attribute and ensures its value is allowed.
Source code
function wp_kses_bad_protocol( $content, $allowed_protocols ) { $content = wp_kses_no_null( $content ); // Short-circuit if the string starts with `https://` or `http://`. Most common cases. if ( ( str_starts_with( $content, 'https://' ) && in_array( 'https', $allowed_protocols, true ) ) || ( str_starts_with( $content, 'http://' ) && in_array( 'http', $allowed_protocols, true ) ) ) { return $content; } $iterations = 0; do { $original_content = $content; $content = wp_kses_bad_protocol_once( $content, $allowed_protocols ); } while ( $original_content !== $content && ++$iterations < 6 ); if ( $original_content !== $content ) { return ''; } return $content;}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 6.9.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.