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:,, In order get ID we need use regular expression.

Get YouTube or Vimeo video ID

Problem: How to correct display YouTube or Vimeo videos.

Solution: Add next code to functions.php:


function display_youtube_or_vimeo_video( $video_url ) {
	if ( strpos( $video_url, '' ) !== false ) {
		$video_id = preg_replace( '/[^\/]+[^0-9]|(\/)/', '', rtrim( $video_url, '/' ) );
		$video_url = '' . $video_id;
	} elseif ( strpos( $video_url, '' ) || 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 = '' . $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;



Leave Comment

2 Replies to “Get YouTube or Vimeo video ID”

Your email address will not be published. Required fields are marked *