youtube_embed_to_short_code

The Jetpack by WordPress.com youtube embed to short code function.

Description

youtube_embed_to_short_code( $content ); 

Parameters (1)

0. $content
The content.

Usage

  1. if ( !function_exists( 'youtube_embed_to_short_code' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/shortcodes/youtube.php'; 
  3.  
  4. // The content. 
  5. $content = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = youtube_embed_to_short_code($content); 
  9.  

Defined (1)

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

/modules/shortcodes/youtube.php  
  1. function youtube_embed_to_short_code( $content ) { 
  2. if ( false === strpos( $content, 'youtube.com' ) ) 
  3. return $content; 
  4.  
  5. //older codes 
  6. $regexp = '!<object width="\d+" height="\d+"><param name="movie" value="https?://www\.youtube\.com/v/([^"]+)"></param>(?:<param name="\w+" value="[^"]*"></param>)*<embed src="https?://www\.youtube\.com/v/(.+)" type="application/x-shockwave-flash"(?: \w+="[^"]*")* width="\d+" height="\d+"></embed></object>!i'; 
  7. $regexp_ent = htmlspecialchars( $regexp, ENT_NOQUOTES ); 
  8. $old_regexp = '!<embed(?:\s+\w+="[^"]*")*\s+src="https?(?:\:|�*58;)//www\.youtube\.com/v/([^"]+)"(?:\s+\w+="[^"]*")*\s*(?:/>|>\s*</embed>)!'; 
  9. $old_regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $old_regexp, ENT_NOQUOTES ) ); 
  10.  
  11. //new code 
  12. $ifr_regexp = '!<iframe((?:\s+\w+="[^"]*")*?)\s+src="(https?:)?//(?:www\.)*youtube.com/embed/([^"]+)".*?</iframe>!i'; 
  13. $ifr_regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $ifr_regexp, ENT_NOQUOTES ) ); 
  14.  
  15. foreach ( array( 'regexp', 'regexp_ent', 'old_regexp', 'old_regexp_ent', 'ifr_regexp', 'ifr_regexp_ent' ) as $reg ) { 
  16. if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) 
  17. continue; 
  18.  
  19. foreach ( $matches as $match ) { 
  20. // Hack, but '?' should only ever appear once, and 
  21. // it should be for the 1st field-value pair in query string,  
  22. // if it is present 
  23. // YouTube changed their embed code. 
  24. // Example of how it is now: 
  25. // <object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/aP9AaD4tgBY?fs=1&hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/aP9AaD4tgBY?fs=1&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object> 
  26. // As shown at the start of function, previous YouTube didn't '?' 
  27. // the 1st field-value pair. 
  28. if ( in_array ( $reg, array( 'ifr_regexp', 'ifr_regexp_ent' ) ) ) { 
  29. $params = $match[1]; 
  30.  
  31. if ( 'ifr_regexp_ent' == $reg ) 
  32. $params = html_entity_decode( $params ); 
  33.  
  34. $params = wp_kses_hair( $params, array( 'http' ) ); 
  35.  
  36. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0; 
  37. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0; 
  38. $wh = ''; 
  39.  
  40. if ( $width && $height ) 
  41. $wh = "&w=$width&h=$height"; 
  42.  
  43. $url = esc_url_raw( "https://www.youtube.com/watch?v={$match[3]}{$wh}" ); 
  44. } else { 
  45. $match[1] = str_replace( '?', '&', $match[1] ); 
  46.  
  47. $url = esc_url_raw( "https://www.youtube.com/watch?v=" . html_entity_decode( $match[1] ) ); 
  48.  
  49. $content = str_replace( $match[0], "[youtube $url]", $content ); 
  50.  
  51. /** 
  52. * Fires before the YouTube embed is transformed into a shortcode. 
  53. * @module shortcodes 
  54. * @since 1.2.0 
  55. * @param string youtube Shortcode name. 
  56. * @param string $url YouTube video URL. 
  57. */ 
  58. do_action( 'jetpack_embed_to_shortcode', 'youtube', $url ); 
  59.  
  60. return $content;