wppaste
WordPress

WP_oEmbed::discover( string $url ): string|false

Since
2.9.0
Source
wp-includes/class-wp-oembed.php:441
Attempts to discover link tags at the given URL for an oEmbed provider.

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

Reaches an outbound HTTP request via wp_safe_remote_get().

Scaling
Scales with input

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

Instructions
23–83

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

Plugin surface
2 hooks

Third-party callbacks on 'oembed_remote_get_args', 'oembed_linktypes' run inside this call, and their cost is not bounded by anything here.

Called by
1

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • httpoutbound HTTP requestwp_safe_remote_get()called directly
  • regexregular expression over the whole inputpreg_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.

WhenInstructionsCalls it makes
always23–25apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body()
always37–43apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos()
always43–53apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos(), preg_match_all()
!$linktypes && stripos()45–50apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos(), stripos()
!$linktypes && stripos()51–60apply_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–76apply_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–83apply_filters(), wp_safe_remote_get(), wp_remote_retrieve_body(), apply_filters(), stripos(), stripos(), preg_match_all(), shortcode_parse_atts(), htmlspecialchars_decode()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8623–8315
8.58623–8315
8.48623–83153 fewer instructions than PHP 8.3
8.38923–8615
8.28923–8615
8.18923–8615
7.48923–8615

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:

  1. apply_filters( oembed_remote_get_args )filterline 457 (+16 into the body)

    Filters oEmbed remote get arguments.

  2. apply_filters( oembed_linktypes )filterline 473 (+32 into the body)

    Filters the link types that contain oEmbed provider URLs.

Uses · 5

Used by · 1

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.

  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.

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.