sovetit

Working with YouTube links in WordPress

Oct 18th, 2021 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Get YouTube miniature
  4.  * @see sovetit_post_thumbnail_youtube
  5.  *
  6.  * @param $post_id
  7.  * @return false|string
  8.  * @author Pavel Ketov <pavel@sovetit.ru>
  9.  * @copyright Copyright (c) 2021, SoveTit RU
  10.  */
  11. function sovetit_post_thumbnail_youtube( $post_id ) {
  12.     if( has_post_thumbnail( $post_id ) ) {
  13.         return get_the_post_thumbnail_url( $post_id, 'full' );
  14.     } else {
  15.         $url_youtube = get_post_meta( $post_id, '_url-youtube', true );
  16.         $id = sovetit_youtube_id( $url_youtube );
  17.         if ( $id ) {
  18.             return "https://img.youtube.com/vi/$id/maxresdefault.jpg";
  19.         } else {
  20.             return false;
  21.         }
  22.     }
  23. }
  24.  
  25. /**
  26.  * Get YouTube iframe
  27.  * @see sovetit_post_iframe_youtube
  28.  *
  29.  * @param $post_id
  30.  * @param int $width
  31.  * @param int $height
  32.  * @return false|string
  33.  * @author Pavel Ketov <pavel@sovetit.ru>
  34.  * @copyright Copyright (c) 2021, SoveTit RU
  35.  */
  36. function sovetit_post_iframe_youtube( $post_id , $width = 720, $height = 405 ) {
  37.     $url_youtube = get_post_meta( $post_id, '_url-youtube', true );
  38.     if ( ! empty( $url_youtube ) ) {
  39.         return trim('
  40.            <iframe
  41.            width="' . $width .'"
  42.            height="' . $height .'"
  43.            src="' . sovetit_live_iframe_youtube( $url_youtube ) . '"
  44.            frameborder="0"
  45.            allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"
  46.            allowfullscreen
  47.            ></iframe>
  48.        ');
  49.     } else {
  50.         return false;
  51.     }
  52. }
  53.  
  54. /**
  55.  * Get YouTube in embed from href
  56.  * @see sovetit_live_iframe_youtube
  57.  *
  58.  * @param $video_href
  59.  * @return false|string
  60.  * @author Pavel Ketov <pavel@sovetit.ru>
  61.  * @copyright Copyright (c) 2021, SoveTit RU
  62.  */
  63. function sovetit_live_iframe_youtube( $video_href ) {
  64.     $id = sovetit_youtube_id( $video_href );
  65.     if ( $id ) {
  66.         return 'https://www.youtube.com/embed/' . $id;
  67.     } else {
  68.         return false;
  69.     }
  70. }
  71.  
  72. /**
  73.  * Get YouTube ID
  74.  * @see sovetit_youtube_id
  75.  *
  76.  * @param $url
  77.  * @return false|string
  78.  * @author Pavel Ketov <pavel@sovetit.ru>
  79.  * @copyright Copyright (c) 2021, SoveTit RU
  80.  */
  81. function sovetit_youtube_id( $url ) {
  82.     $youtube    = parse_url( $url );
  83.     $host       = $youtube['host'];
  84.     if ( $host === 'www.youtube.com' || $host === 'youtube.com' ) {
  85.         parse_str( trim(  $youtube['query'] ), $v );
  86.         $id = trim( $v['v'] );
  87.     } else if ( $host === 'youtu.be' ) {
  88.         $id = substr($youtube['path'], 1);
  89.     } else {
  90.         return false;
  91.     }
  92.     return $id;
  93. }
Add Comment
Please, Sign In to add comment