get_oembed_response_data_for_url()WP 5.0.0

Retrieves the oEmbed response data for a given URL.

Hooks from the function

Return

Object|false. oEmbed response data if the URL does belong to the current site. False otherwise.

Usage

get_oembed_response_data_for_url( $url, $args );
$url(string) (required)
The URL that should be inspected for discovery <link> tags.
$args(array) (required)
oEmbed remote get arguments.

Examples

0

#1 Demo

$url = 'https://example.com/5875/30-css-selektorov';

$data = get_oembed_response_data_for_url( $url, [ 'width' => 400 ] );

print_r( $data );

We get it:

stdClass Object
(
	[version] => 1.0
	[provider_name] => WordPress as the palm of your hand
	[provider_url] => https://example.com
	[author_name] => Kama
	[author_url] => https://example.com/author/kama
	[title] => Selectors in CSS
	[type] => rich
	[width] => 400
	[height] => 225
	[html] => <blockquote class="wp-embedded-content"><a href="/5875/30-css-selektorov"
		>Selectors in CSS</a></blockquote>
		<script type='text/javascript'>
		<!--//--><![CDATA[//><!--
				/*! This file is auto-generated */
				!function(c,d){"use strict";var e=!1,n=!1;if(d.querySelector)if(c.addEventListener)e=!0;if(c.wp=c.wp||
				{},!c.wp.receiveEmbedMessage)if(c.wp.receiveEmbedMessage=function(e){var t=e.data;if(t)if(t.secret||t.message||t.value)
				if(!/[^a-zA-Z0-9]/.test(t.secret)){for(var r,a,i,s=d.querySelectorAll('iframe[data-secret="'+t.secret+'"]'),
				n=d.querySelectorAll('blockquote[data-secret="'+t.secret+'"]'),o=0;o<n.length;o++)n[o].style.display="none";
				for(o=0;o<s.length;o++)if(r=s[o],e.source===r.contentWindow){if(r.removeAttribute("style"),"height"===t.message){
				if(1e3<(i=parseInt(t.value,10)))i=1e3;else if(~~i<200)i=200;r.height=i}if("link"===t.message)
				if(a=d.createElement("a"),i=d.createElement("a"),a.href=r.getAttribute("src"),i.href=t.value,i.host===a.host)
				if(d.activeElement===r)c.top.location.href=t.value}}},e)c.addEventListener("message",c.wp.receiveEmbedMessage,!1),
				d.addEventListener("DOMContentLoaded",t,!1),c.addEventListener("load",t,!1);function t(){if(!n){n=!0;
				for(var e,t,r=-1!==navigator.appVersion.indexOf("MSIE 10"),a=!!navigator.userAgent.match(/Trident.*rv:11\./),
				i=d.querySelectorAll("iframe.wp-embedded-content"),s=0;s<i.length;s++){
				if(!(e=i[s]).getAttribute("data-secret"))t=Math.random().toString(36).substr(2,10),
				e.src+="#?secret="+t,e.setAttribute("data-secret",t);if(r||a)(t=e.cloneNode(!0)).removeAttribute("security"),
				e.parentNode.replaceChild(t,e)}}}}(window,document);
		//--><!]]>
		</script><iframe sandbox="allow-scripts" security="restricted"
		src="/id_5875/30-css-selektorov.html/embed" width="400" height="225"
		title=""Selectors in CSS - WordPress as you see it" frameborder="0"
		marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>
	[thumbnail_url] => https://example.com/wp-content/uploads/2015/06/selektory-css.png
	[thumbnail_width] => 400
	[thumbnail_height] => 376
)
0

#2 Remove oEmbed embedding for internal site links (current site URL)

For example. We need embedding not to work when we specify in the post a link to the current site on a separate line. To do this we need to disable such hook:

// Remove filter of the oEmbed result before any HTTP requests are made.
remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 );

Changelog

Since 5.0.0 Introduced.

get_oembed_response_data_for_url() code WP 6.5.2

function get_oembed_response_data_for_url( $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'                   => '/',
			'update_site_meta_cache' => false,
		);

		// 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 false;
	}

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

	$data = get_oembed_response_data( $post_id, $width );

	if ( $switched_blog ) {
		restore_current_blog();
	}

	return $data ? (object) $data : false;
}