get_post_embed_html( int $width, int $height, int|WP_Post $post = null )
Retrieves the embed code for a specific post.
Parameters
- $width
-
(int) (Required) The width for the response.
- $height
-
(int) (Required) The height for the response.
- $post
-
(int|WP_Post) (Optional) Post ID or object. Default is global
$post
.Default value: null
Return
(string|false) Embed code on success, false if post doesn't exist.
Source
File: wp-includes/embed.php
function get_post_embed_html( $width, $height, $post = null ) { $post = get_post( $post ); if ( ! $post ) { return false; } $embed_url = get_post_embed_url( $post ); $output = '<blockquote class="wp-embedded-content"><a href="' . esc_url( get_permalink( $post ) ) . '">' . get_the_title( $post ) . "</a></blockquote>\n"; $output .= "<script type='text/javascript'>\n"; $output .= "<!--//--><![CDATA[//><!--\n"; if ( SCRIPT_DEBUG ) { $output .= file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' ); } else { /* * If you're looking at a src version of this file, you'll see an "include" * statement below. This is used by the `npm run build` process to directly * include a minified version of wp-embed.js, instead of using the * file_get_contents() method from above. * * If you're looking at a build version of this file, you'll see a string of * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG * and edit wp-embed.js directly. */ $output .= <<<JS /*! 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); JS; } $output .= "\n//--><!]]>"; $output .= "\n</script>"; $output .= sprintf( '<iframe sandbox="allow-scripts" security="restricted" src="%1$s" width="%2$d" height="%3$d" title="%4$s" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" class="wp-embedded-content"></iframe>', esc_url( $embed_url ), absint( $width ), absint( $height ), esc_attr( sprintf( /* translators: 1: Post title, 2: Site title. */ __( '“%1$s” — %2$s' ), get_the_title( $post ), get_bloginfo( 'name' ) ) ) ); /** * Filters the embed HTML output for a given post. * * @since 4.4.0 * * @param string $output The default iframe tag to display embedded content. * @param WP_Post $post Current post object. * @param int $width Width of the response. * @param int $height Height of the response. */ return apply_filters( 'embed_html', $output, $post, $width, $height ); }
Changelog
Version | Description |
---|---|
4.4.0 | Introduced. |
© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_post_embed_html