get_post_embed_html()
Gets the ready HTML code for embedding the specified post using oEmbed. This code is intended to be used for embedding the post on another resource.
What is Embed read here: oEmbed in WordPress.
Uses: get_post_embed_url()
Hooks from the function
Returns
String|false. HTML code: Embed code. Will return false if the specified post does not exist.
Usage
get_post_embed_html( $width, $height, $post );
- $width(int) (required)
- Width of the embed. Specified for iframe.
- $height(int) (required)
- Height of the embed. Specified for iframe.
- $post(int/WP_Post)
- ID or object of the post whose embed code needs to be retrieved.
Default: null (current post in the loop)
Examples
#1 An example of getting oEmbed HTML code
echo get_post_embed_html( 400, 250, 1 );
The result is:
<blockquote class="wp-embedded-content"><a href="/handbook/codex/data-types/posts">How hooks work in WordPress (filters and events)</a></blockquote>
<script type='text/javascript'>
<!--//--><![CDATA[//><!--
!function(a,b){"use strict";function c(){if(!e){e=!0;var a,c,d,f,g=-1!==navigator.appVersion.indexOf("MSIE 10"),h=!!navigator.userAgent.match(/Trident.*rv:11\./),i=b.querySelectorAll("iframe.wp-embedded-content"),j=b.querySelectorAll("blockquote.wp-embedded-content");for(c=0;c<j.length;c++)j[c].style.display="none";for(c=0;c<i.length;c++)if(d=i[c],d.style.display="",!d.getAttribute("data-secret")){if(f=Math.random().toString(36).substr(2,10),d.src+="#?secret="+f,d.setAttribute("data-secret",f),g||h)a=d.cloneNode(!0),a.removeAttribute("security"),d.parentNode.replaceChild(a,d)}else;}}var d=!1,e=!1;if(b.querySelector)if(a.addEventListener)d=!0;if(a.wp=a.wp||{},!a.wp.receiveEmbedMessage)if(a.wp.receiveEmbedMessage=function(c){var d=c.data;if(d.secret||d.message||d.value)if(!/[^a-zA-Z0-9]/.test(d.secret)){var e,f,g,h,i,j=b.querySelectorAll('iframe[data-secret="'+d.secret+'"]'),k=b.querySelectorAll('blockquote[data-secret="'+d.secret+'"]');for(e=0;e<k.length;e++)k[e].style.display="none";for(e=0;e<j.length;e++)if(f=j[e],c.source===f.contentWindow){if(f.style.display="","height"===d.message){if(g=parseInt(d.value,10),g>1e3)g=1e3;else if(200>~~g)g=200;f.height=g}if("link"===d.message)if(h=b.createElement("a"),i=b.createElement("a"),h.href=f.getAttribute("src"),i.href=d.value,i.host===h.host)if(b.activeElement===f)a.top.location.href=d.value}else;}},d)a.addEventListener("message",a.wp.receiveEmbedMessage,!1),b.addEventListener("DOMContentLoaded",c,!1),a.addEventListener("load",c,!1)}(window,document);
//--><!]]>
</script>
<iframe sandbox="allow-scripts" security="restricted" src="/handbook/codex/data-types/posts/embed" width="400" height="300" title="Embedded WordPress post" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>
And this is what it will look like on the page:
Changelog
| Since 4.4.0 | Introduced. |