get_post_embed_html

Retrieves the embed code for a specific post.

Description

(string|false) get_post_embed_html( (int) $width, (int) $height, (null) $post = null ); 

Returns (string|false)

Embed code on success, false if post doesn't exist.

Parameters (3)

0. $width (int)
The width for the response.
1. $height (int)
The height for the response.
2. $post — Optional. (null) => null
Post ID or object. Default is global $post..

Usage

  1. if ( !function_exists( 'get_post_embed_html' ) ) { 
  2. require_once ABSPATH . WPINC . '/embed.php'; 
  3.  
  4. // The width for the response. 
  5. $width = -1; 
  6.  
  7. // The height for the response. 
  8. $height = -1; 
  9.  
  10. // Optional. Post ID or object. Default is global `$post`. 
  11. $post = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_post_embed_html($width, $height, $post); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/embed.php  
  1. function get_post_embed_html( $width, $height, $post = null ) { 
  2. $post = get_post( $post ); 
  3.  
  4. if ( ! $post ) { 
  5. return false; 
  6.  
  7. $embed_url = get_post_embed_url( $post ); 
  8.  
  9. $output = '<blockquote class="wp-embedded-content"><a href="' . esc_url( get_permalink( $post ) ) . '">' . get_the_title( $post ) . "</a></blockquote>\n"; 
  10.  
  11. $output .= "<script type='text/javascript'>\n"; 
  12. $output .= "<!--//--><![CDATA[//><!--\n"; 
  13. if ( SCRIPT_DEBUG ) { 
  14. $output .= file_get_contents( ABSPATH . WPINC . '/js/wp-embed.js' ); 
  15. } else { 
  16. /** 
  17. * If you're looking at a src version of this file, you'll see an "include" 
  18. * statement below. This is used by the `grunt build` process to directly 
  19. * include a minified version of wp-embed.js, instead of using the 
  20. * file_get_contents() method from above. 
  21. * If you're looking at a build version of this file, you'll see a string of 
  22. * minified JavaScript. If you need to debug it, please turn on SCRIPT_DEBUG 
  23. * and edit wp-embed.js directly. 
  24. */ 
  25. $output .=<<<JS 
  26. !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");for(c=0;c<i.length;c++) {if(d=i[c], !d.getAttribute("data-secret"))f=Math.random().toString(36).substr(2, 10), d.src+="#?secret="+f, d.setAttribute("data-secret", f);if(g||h)a=d.cloneNode(!0), a.removeAttribute("security"), d.parentNode.replaceChild(a, d)}}}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.removeAttribute("style"), "height"===d.message) {if(g=parseInt(d.value, 10), g>1e3)g=1e3;else if(~~g<200)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); 
  27. JS; 
  28. $output .= "\n//--><!]]>"; 
  29. $output .= "\n</script>"; 
  30.  
  31. $output .= sprintf( 
  32. '<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>',  
  33. esc_url( $embed_url ),  
  34. absint( $width ),  
  35. absint( $height ),  
  36. sprintf( 
  37. /** translators: 1: post title, 2: site name */ 
  38. __( '“%1$s” — %2$s' ),  
  39. get_the_title( $post ),  
  40. get_bloginfo( 'name' ) 
  41. ); 
  42.  
  43. /** 
  44. * Filters the embed HTML output for a given post. 
  45. * @since 4.4.0 
  46. * @param string $output The default HTML. 
  47. * @param WP_Post $post Current post object. 
  48. * @param int $width Width of the response. 
  49. * @param int $height Height of the response. 
  50. */ 
  51. return apply_filters( 'embed_html', $output, $post, $width, $height );