get_oembed_response_data_for_url()
Gets oEmbed data for the current site's URL.
The purpose of this function is to try to find a post on the current site that corresponds to the given URL. If such a post is found, an embed response will be created for it (as if we received a response from the route for embedding content).
By default, this function is hooked to the pre_oembed_result:
add_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10, 3 );
The purpose of this hook is to process the URL in the content during the embedding process, and if it is a URL to a post on the current site, to form an embed response without additional HTTP discover requests.
Hooks from the function
Returns
Object|false. oEmbed data (the same as the HTTP response for the oEmbed route), if the URL belongs to the current site. False otherwise.
Usage
get_oembed_response_data_for_url( $url, $args );
- $url(string) (required)
- URL to check if it is a link to a post on the current site.
- $args(array) (required)
- oEmbed response parameters. One parameter
widthis supported - the width of the iframe.
Examples
#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
) #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. |