Advertisement
phpface

Untitled

May 26th, 2021
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  1. /**
  2.  *
  3.  * Get youtube video ID
  4.  *
  5.  * @param  string $url
  6.  * @return true|false
  7.  *
  8.  * @since  1.0.0
  9.  *
  10.  */
  11. function videotube_child_get_youtube_id( $url ){
  12.    
  13.     preg_match( '/(youtube.com\/watch\?v=|youtu.be\/|youtube.com\/embed\/)(?P<id>.{11})/', $url, $matched );
  14.  
  15.     if( is_array( $matched ) && array_key_exists( 'id' , $matched ) ){
  16.         return $matched['id'];
  17.     }
  18.  
  19.     return false;
  20. }
  21.  
  22. /**
  23.  *
  24.  * Convert youtube link to iframe
  25.  *
  26.  * @param   $output
  27.  * @param  [type] $media_object
  28.  * @since  1.0.0
  29.  *
  30.  */
  31. function videotube_child_filter_player( $output, $media_object ){
  32.  
  33.     if( "" != $youtubeId = videotube_child_get_youtube_id( $media_object ) ){
  34.         $output = sprintf(
  35.             '<iframe src="https://www.youtube.com/embed/%s?autoplay=1" frameborder="0" allowfullscreen="allowfullscreen"></iframe>',
  36.             $youtubeId
  37.         );
  38.     }
  39.  
  40.     return $output;
  41.  
  42. }
  43. add_filter( 'videotube_player', 'videotube_child_filter_player', 100, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement