wp_filter_pre_oembed_result( null|string $result, string $url, array $args )

Filters the oEmbed result before any HTTP requests are made.


Description

If the URL belongs to the current site, the result is fetched directly instead of going through the oEmbed discovery process.


Parameters

$result

(null|string) (Required) The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Default null.

$url

(string) (Required) The URL that should be inspected for discovery <link> tags.

$args

(array) (Required) oEmbed remote get arguments.


Return

(null|string) The UNSANITIZED (and potentially unsafe) HTML that should be used to embed. Null if the URL does not belong to the current site.


Source

File: wp-includes/embed.php

function wp_filter_pre_oembed_result( $result, $url, $args ) {
	$switched_blog = false;

	if ( is_multisite() ) {
		$url_parts = wp_parse_args( wp_parse_url( $url ), array(
			'host'   => '',
			'path'   => '/',
		) );

		$qv = array( 'domain' => $url_parts['host'], 'path' => '/' );

		// In case of subdirectory configs, set the path.
		if ( ! is_subdomain_install() ) {
			$path = explode( '/', ltrim( $url_parts['path'], '/' ) );
			$path = reset( $path );

			if ( $path ) {
				$qv['path'] = get_network()->path . $path . '/';
			}
		}

		$sites = get_sites( $qv );
		$site  = reset( $sites );

		// Do not allow embeds for deleted/archived/spam sites.
		if ( ! empty( $site->deleted ) || ! empty( $site->spam ) || ! empty( $site->archived ) ) {
			return false;
		}

		if ( $site && get_current_blog_id() !== (int) $site->blog_id ) {
			switch_to_blog( $site->blog_id );
			$switched_blog = true;
		}
	}

	$post_id = url_to_postid( $url );

	/** This filter is documented in wp-includes/class-wp-oembed-controller.php */
	$post_id = apply_filters( 'oembed_request_post_id', $post_id, $url );

	if ( ! $post_id ) {
		if ( $switched_blog ) {
			restore_current_blog();
		}

		return $result;
	}

	$width = isset( $args['width'] ) ? $args['width'] : 0;

	$data = get_oembed_response_data( $post_id, $width );
	$data = _wp_oembed_get_object()->data2html( (object) $data, $url );

	if ( $switched_blog ) {
		restore_current_blog();
	}

	if ( ! $data ) {
		return $result;
	}

	return $data;
}


Changelog

Changelog
Version Description
WP-4.5.3 Introduced.