shortcode->getHandlers()->add('yt', function(ShortcodeInterface $sc) { // Get shortcode content and parameters $url = $sc->getContent(); $params = $sc->getParameters(); $privacy_mode = $sc->getParameter('privacy_enhanced_mode'); if ($url) { preg_match($this::YOUTUBE_REGEX, $url, $matches); $search = $matches[0]; // double check to make sure we found a valid YouTube video ID if (!isset($matches[1])) { return $search; } /** @var Twig $twig */ $twig = $this->grav['twig']; // build the replacement embed HTML string $replace = $twig->processTemplate('partials/yt.html.twig', array( 'player_parameters' => $params, 'privacy_enhanced_mode' => $privacy_mode, 'video_id' => $matches[1], )); // do the replacement return str_replace($search, $replace, $search); } }); } }