WP_oEmbed::discover( string $url ): string|false
- Since
- 2.9.0
- Source
wp-includes/class-wp-oembed.php:441
Compatibility
- WordPress
- since 2.9.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
$urlstring- The URL that should be inspected for discovery
<link>tags.
Return value
string|false- The oEmbed provider URL on success, false on failure.
Performance profile
How much work a call to WP_oEmbed::discover() 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
- Expensive
- Scaling
- Scales with input
- Instructions
- 23–83
- Plugin surface
- 2 hooks
- Called by
- 1
Reaches an outbound HTTP request via wp_safe_remote_get().
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 86.
Third-party callbacks on 'oembed_remote_get_args', 'oembed_linktypes' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - httpoutbound HTTP request
wp_safe_remote_get()called directly - regexregular expression over the whole input
preg_match_all()called directly
What one call costs · 7 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_oEmbed::discover() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 23–25 | apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body() |
| always | 37–43 | apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos() |
| always | 43–53 | apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos(), preg_match_all() |
!$linktypes && stripos() | 45–50 | apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos(), stripos() |
!$linktypes && stripos() | 51–60 | apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos(), stripos(), preg_match_all() |
preg_match_all() && !$links && !empty($atts) && !empty($linktypes) | 70–76 | apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos(), preg_match_all(), shortcode_parse_atts(), htmlspecialchars_decode() |
!$linktypes && stripos() && preg_match_all() && !$links && !empty($atts) && !empty($linktypes) | 78–83 | apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos(), stripos(), preg_match_all(), shortcode_parse_atts(), htmlspecialchars_decode() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 86 | 23–83 | 15 | |
| 8.5 | 86 | 23–83 | 15 | |
| 8.4 | 86 | 23–83 | 15 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 89 | 23–86 | 15 | |
| 8.2 | 89 | 23–86 | 15 | |
| 8.1 | 89 | 23–86 | 15 | |
| 7.4 | 89 | 23–86 | 15 |
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.
Hooks and filters fired · 2
2 hooks fire while WP_oEmbed::discover() runs, in this order:
- apply_filters( oembed_remote_get_args )filterline 457 (+16 into the body)
Filters oEmbed remote get arguments.
- apply_filters( oembed_linktypes )filterline 473 (+32 into the body)
Filters the link types that contain oEmbed provider URLs.
Uses · 5
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_safe_remote_get()Retrieves the raw response from a safe HTTP request using the GET method.
- wp_remote_retrieve_body()Retrieves only the body from the raw response.
- stripos()
- shortcode_parse_atts()Retrieves all attributes from the shortcodes tag.
Used by · 1
- WP_oEmbed::get_provider()Takes a URL and returns the corresponding oEmbed provider's URL, if there is one.
Source code
public function discover( $url ) { $providers = array(); $args = array( 'limit_response_size' => 153600, // 150 KB ); /** * Filters oEmbed remote get arguments. * * @since 4.0.0 * * @see WP_Http::request() * * @param array $args oEmbed remote get arguments. * @param string $url URL to be inspected. */ $args = apply_filters( 'oembed_remote_get_args', $args, $url ); // Fetch URL content. $request = wp_safe_remote_get( $url, $args ); $html = wp_remote_retrieve_body( $request ); if ( $html ) { /** * Filters the link types that contain oEmbed provider URLs. * * @since 2.9.0 * * @param string[] $format Array of oEmbed link types. Accepts 'application/json+oembed', * 'text/xml+oembed', and 'application/xml+oembed' (incorrect, * used by at least Vimeo). */ $linktypes = apply_filters( 'oembed_linktypes', array( 'application/json+oembed' => 'json', 'text/xml+oembed' => 'xml', 'application/xml+oembed' => 'xml', ) ); // Strip <body>. $html_head_end = stripos( $html, '</head>' ); if ( $html_head_end ) { $html = substr( $html, 0, $html_head_end ); } // Do a quick check. $tagfound = false; foreach ( $linktypes as $linktype => $format ) { if ( stripos( $html, $linktype ) ) { $tagfound = true; break; } } if ( $tagfound && preg_match_all( '#<link([^<>]+)/?>#iU', $html, $links ) ) { foreach ( $links[1] as $link ) { $atts = shortcode_parse_atts( $link ); if ( ! empty( $atts['type'] ) && ! empty( $linktypes[ $atts['type'] ] ) && ! empty( $atts['href'] ) ) { $providers[ $linktypes[ $atts['type'] ] ] = htmlspecialchars_decode( $atts['href'] ); // Stop here if it's JSON (that's all we need). if ( 'json' === $linktypes[ $atts['type'] ] ) { break; } } } } } // JSON is preferred to XML. if ( ! empty( $providers['json'] ) ) { return $providers['json']; } elseif ( ! empty( $providers['xml'] ) ) { return $providers['xml']; } else { return false; }Changelog
Introduced in 2.9.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.8.8 tag, from
src/wp-includes/class-wp-oembed.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.