wp_filter_oembed_result( string $result, object $data, string $url ): string
- Since
- 4.4.0
- Source
wp-includes/embed.php:918
Description
If the $url isn't on the trusted providers list, we need to filter the HTML heavily for security.
Only filters 'rich' and 'video' response types.
Compatibility
- WordPress
- since 4.4.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
$resultstring- The oEmbed HTML result.
$dataobject- A data object result from an oEmbed provider.
$urlstring- The URL of the content to be embedded.
Return value
string- The filtered and sanitized oEmbed result.
Performance profile
How much work a call to wp_filter_oembed_result() 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
- 6–98
- Plugin surface
- None
- Called by
- 0
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 101.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_filter_oembed_result()
Further down the call graph this can also reach transient, cache, option, serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_filter_oembed_result() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–9 | none |
$result !== false | 18 | _wp_oembed_get_object(), ->get_provider() |
$result !== false && empty($content) | 31 | _wp_oembed_get_object(), ->get_provider(), wp_kses(), preg_match() |
$result !== false && !empty($content) && empty($results) | 61–67 | _wp_oembed_get_object(), ->get_provider(), wp_kses(), preg_match(), preg_match(), wp_kses(), str_ireplace() |
$result !== false && !empty($content) && !empty($results) | 92–98 | _wp_oembed_get_object(), ->get_provider(), wp_kses(), preg_match(), preg_match(), wp_generate_password(), esc_url(), wp_kses(), str_ireplace() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 101 | 6–98 | 6 | |
| 8.5 | 101 | 6–98 | 6 | |
| 8.4 | 101 | 6–98 | 6 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 113 | 6–110 | 6 | |
| 8.2 | 113 | 6–110 | 6 | |
| 8.1 | 113 | 6–110 | 6 | |
| 7.4 | 113 | 6–110 | 6 |
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 · 4
- _wp_oembed_get_object()Returns the initialized WP_oEmbed object.
- wp_kses()Filters text content and strips out disallowed HTML.
- wp_generate_password()Generates a random password drawn from the defined set of characters.
- esc_url()Checks and cleans a URL.
Source code
function wp_filter_oembed_result( $result, $data, $url ) { if ( false === $result || ! in_array( $data->type, array( 'rich', 'video' ), true ) ) { return $result; } $wp_oembed = _wp_oembed_get_object(); // Don't modify the HTML for trusted providers. if ( false !== $wp_oembed->get_provider( $url, array( 'discover' => false ) ) ) { return $result; } $allowed_html = array( 'a' => array( 'href' => true, ), 'blockquote' => array(), 'iframe' => array( 'src' => true, 'width' => true, 'height' => true, 'frameborder' => true, 'marginwidth' => true, 'marginheight' => true, 'scrolling' => true, 'title' => true, ), ); $html = wp_kses( $result, $allowed_html ); preg_match( '|(<blockquote>.*?</blockquote>)?.*(<iframe.*?></iframe>)|ms', $html, $content ); // We require at least the iframe to exist. if ( empty( $content[2] ) ) { return false; } $html = $content[1] . $content[2]; preg_match( '/ src=([\'"])(.*?)\1/', $html, $results ); if ( ! empty( $results ) ) { $secret = wp_generate_password( 10, false ); $url = esc_url( "{$results[2]}#?secret=$secret" ); $q = $results[1]; $html = str_replace( $results[0], ' src=' . $q . $url . $q . ' data-secret=' . $q . $secret . $q, $html ); $html = str_replace( '<blockquote', "<blockquote data-secret=\"$secret\"", $html ); } $allowed_html['blockquote']['data-secret'] = true; $allowed_html['iframe']['data-secret'] = true; $html = wp_kses( $html, $allowed_html ); if ( ! empty( $content[1] ) ) { // We have a blockquote to fall back on. Hide the iframe by default. $html = str_replace( '<iframe', '<iframe style="position: absolute; visibility: hidden;"', $html ); $html = str_replace( '<blockquote', '<blockquote class="wp-embedded-content"', $html ); } $html = str_ireplace( '<iframe', '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"', $html ); return $html;}Changelog
Introduced in 4.4.0. 2 changes between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$result retyped from string to string|false.verified against sourcestring to string|false.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/embed.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.