Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Get YouTube miniature
- * @see sovetit_post_thumbnail_youtube
- *
- * @param $post_id
- * @return false|string
- * @author Pavel Ketov <pavel@sovetit.ru>
- * @copyright Copyright (c) 2021, SoveTit RU
- */
- function sovetit_post_thumbnail_youtube( $post_id ) {
- if( has_post_thumbnail( $post_id ) ) {
- return get_the_post_thumbnail_url( $post_id, 'full' );
- } else {
- $url_youtube = get_post_meta( $post_id, '_url-youtube', true );
- $id = sovetit_youtube_id( $url_youtube );
- if ( $id ) {
- return "https://img.youtube.com/vi/$id/maxresdefault.jpg";
- } else {
- return false;
- }
- }
- }
- /**
- * Get YouTube iframe
- * @see sovetit_post_iframe_youtube
- *
- * @param $post_id
- * @param int $width
- * @param int $height
- * @return false|string
- * @author Pavel Ketov <pavel@sovetit.ru>
- * @copyright Copyright (c) 2021, SoveTit RU
- */
- function sovetit_post_iframe_youtube( $post_id , $width = 720, $height = 405 ) {
- $url_youtube = get_post_meta( $post_id, '_url-youtube', true );
- if ( ! empty( $url_youtube ) ) {
- return trim('
- <iframe
- width="' . $width .'"
- height="' . $height .'"
- src="' . sovetit_live_iframe_youtube( $url_youtube ) . '"
- frameborder="0"
- allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"
- allowfullscreen
- ></iframe>
- ');
- } else {
- return false;
- }
- }
- /**
- * Get YouTube in embed from href
- * @see sovetit_live_iframe_youtube
- *
- * @param $video_href
- * @return false|string
- * @author Pavel Ketov <pavel@sovetit.ru>
- * @copyright Copyright (c) 2021, SoveTit RU
- */
- function sovetit_live_iframe_youtube( $video_href ) {
- $id = sovetit_youtube_id( $video_href );
- if ( $id ) {
- return 'https://www.youtube.com/embed/' . $id;
- } else {
- return false;
- }
- }
- /**
- * Get YouTube ID
- * @see sovetit_youtube_id
- *
- * @param $url
- * @return false|string
- * @author Pavel Ketov <pavel@sovetit.ru>
- * @copyright Copyright (c) 2021, SoveTit RU
- */
- function sovetit_youtube_id( $url ) {
- $youtube = parse_url( $url );
- $host = $youtube['host'];
- if ( $host === 'www.youtube.com' || $host === 'youtube.com' ) {
- parse_str( trim( $youtube['query'] ), $v );
- $id = trim( $v['v'] );
- } else if ( $host === 'youtu.be' ) {
- $id = substr($youtube['path'], 1);
- } else {
- return false;
- }
- return $id;
- }
Add Comment
Please, Sign In to add comment