Nice one!
May I just suggest one tiny improvement? Don't force admins to "extract" the video ID - it's a waste of time and leaves room for error (by accidentally deleting a character). Allowing a copy/paste from the address bar or using the Share button on YT makes it easier and much faster for the admin... Here's a function I use on multiple sites (tested):
Code:
function extractYouTubeID($url) {
// If it's already an ID (no "http" in it), return as is
if (preg_match('/^[a-zA-Z0-9_-]{11}$/', $url)) {
return $url;
}
// Parse the URL
$parsedUrl = parse_url($url);
// Check for short YouTube URL
if (isset($parsedUrl['host']) && $parsedUrl['host'] === 'youtu.be' && isset($parsedUrl['path'])) {
return ltrim($parsedUrl['path'], '/');
}
if (!isset($parsedUrl['query'])) {
return false; // Invalid format
}
// Parse query parameters
parse_str($parsedUrl['query'], $queryParams);
// Return the video ID if it exists
return $queryParams['v'] ?? false;
}
You can place it in your observer file and simply call it instead of
Code:
$youtube_video_id = preg_replace('/[^a-zA-Z0-9_-]/', '', $youtube_video_id);