method_parse_video_url( $url )

Identifies the provider and video id in a YouTube or Vimeo URL.


Description

Matches the URL against the forms each provider uses and returns the provider and the id the embed URL needs, or null for anything else. YouTube: youtube.com/watch?v= (with other query arguments before v), youtube.com/embed/, youtube.com/shorts/, youtube.com/v/ and youtu.be/, the id being the 11-character token that follows. Vimeo: the numeric id after vimeo.com/, with or without a path before it (vimeo.com/123456, player.vimeo.com/video/123456, vimeo.com/channels/staffpicks/123456).


Parameters

$url (string) :
The video’s page or player URL.


Return

(array|null) array( 'provider' => 'youtube'|'vimeo', 'id' => string ), or null when the URL is empty or matches neither provider.


Source

File: lib/helper-functions.php


Usage

PHP
$parsed = method_parse_video_url( 'https://youtu.be/dQw4w9WgXcQ?t=42' );
// array( 'provider' => 'youtube', 'id' => 'dQw4w9WgXcQ' )

if ( $parsed ) {
    $src = method_build_embed_url( $parsed['provider'], $parsed['id'] );
}

Notes

  • Used by the Fluid Video block, which renders nothing when this returns null.
  • Pairs with method_build_embed_url(), which turns the result into the player URL.
  • The YouTube pattern looks for youtube.com/ plus one of the path forms, so any host that ends in it matches (m.youtube.com included), while youtube-nocookie.com does not; the embed URL built from the id always uses www.youtube.com.

Changelog

VersionDescription
2.0.0-beta14Introduced, with the Fluid Video block (ba36676).