Many users think websites without media (audio, videos, etc) are pretty boring. I really hope, that doesn’t apply to HelloAdmin 😁 That is why, vast majority sites contain media-content. As rule, video content doesn’t placed to self website but for that use video hosting such as YouTube, Vimeo, etc. This choice, dictated by need to promotion resource and of coгrse save hard disk space.
For example, let’s views YouTube case, where each video has unique ID, but URL’s format can be various: https://www.youtube.com/watch?v=ID, https://youtube.com/?v=ID, https://youtu.be/ID. In order get ID we need use regular expression.
Problem: How to correct display YouTube or Vimeo videos.
Solution: Add next code to functions.php:
<?php
function display_youtube_or_vimeo_video( $video_url ) {
if ( strpos( $video_url, 'vimeo.com' ) !== false ) {
$video_id = preg_replace( '/[^\/]+[^0-9]|(\/)/', '', rtrim( $video_url, '/' ) );
$video_url = 'https://player.vimeo.com/video/' . $video_id;
} elseif ( strpos( $video_url, 'youtu.be' ) || strpos( $video_url, 'youtube' ) !== false ) {
preg_match( "/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user|shorts)\/))([^\?&\"'>]+)/", $video_url, $matches );
$video_url = 'https://www.youtube.com/embed/' . $matches[1];
}
$iframe = '<iframe src="' . $video_url . '" title="Video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
echo $iframe;
}
Sources:
I really like it when folks get together and share thoughts.
Great blog, continue the good work!
Very good article. I will be going through many of these issues as well..