将额外的查询字符串添加到由 wp_oembed_get() 函数生成或输出的 iframe 元素的 src 属性值

Add extra query string to src attribute's value of an iframe element that has been generated or outputted by wp_oembed_get() function

我想向 iframe 元素的 src 属性值添加一个额外的查询字符串。我想通过使用 PHP 或 WordPress 而不是 JavaScript.

来实现这一点

问题是我想使用 Youtube iFrame API,只有当 iframe 有一个查询字符串时才有可能,其中 enablejspai=1 src=" https://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1"

目前,用户将复制粘贴视频 URL,我的代码将抓取该视频 URL 并将其转换为 iframe 元素。

$embed_code = wp_oembed_get( $video_post_url);
echo $embed_code;

上面的代码将输出一个带有 src 属性的 iframe 元素,其值为普通 url 但我想向它添加一个查询字符串并使其成为普通 -url?enablejsapi=1所以我可以使用 Youtube Iframe API.

您可以获得url如下:

// Load value.
$iframe = wp_oembed_get( $video_post_url );

// Use preg_match to find iframe src.
preg_match('/src="(.+?)"/', $iframe, $matches);

// Iframe URL
$src = $matches[1];