Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks
Embed::get_youtube_thumbnail
Extract YouTube video thumbnail URL.
Method of the class: Embed{}
No Hooks.
Returns
string. Thumbnail URL or empty string.
Usage
// private - for code of main (parent) class only $result = $this->get_youtube_thumbnail( $url ): string;
- $url(string) (required)
- YouTube video URL.
Embed::get_youtube_thumbnail() Embed::get youtube thumbnail code WC 11.1.2
private function get_youtube_thumbnail( string $url ): string {
// Extract video ID from various YouTube URL formats. The `videoseries`
// token (used by playlist embed URLs like /embed/videoseries?list=…) is
// not a real video ID, so it is excluded to fall through to oEmbed.
$video_id = '';
if ( preg_match( '/(?:youtube\.com\/(?:watch\?v=|shorts\/|live\/|embed\/)|youtu\.be\/)(?!videoseries\b)([a-zA-Z0-9_-]+)/i', $url, $matches ) ) {
$video_id = $matches[1];
}
if ( empty( $video_id ) ) {
return '';
}
// Return YouTube thumbnail URL.
// Using 0.jpg format as shown in the example.
return 'https://img.youtube.com/vi/' . $video_id . '/0.jpg';
}