在 Wordpress 上,我尝试嵌入 YouTube 视频,然后包含跳转到该视频中特定时间的链接。所以如果我想跳到 2:00,我可以创建一个链接,跳到嵌入视频中的那个点。
几年前我发现了一个类似的问题,但我相信 YouTube API 已经改变了。我找不到任何可以做到这一点的插件,所以如果可能的话,我想自己做。
建议?
编辑:这是我到目前为止想出的代码。但是,唯一的问题是,如果视频还没有播放,那么seekTo 似乎需要很长时间。我不确定是否应该响应 onReady 事件或其他事件。
add_action( 'wp_enqueue_scripts', 'register_yt_frameapi' );
function register_yt_frameapi() {
wp_register_script( 'frameapi', 'https://www.youtube.com/iframe_api', array(), '1.0.0', true );
}
add_shortcode('ytlink', 'ytlink');
function ytlink($atts, $content = null)
{
wp_enqueue_script('frameapi');
$id = $atts['id'];
$time = $atts['time'];
$timeParts = explode(':', $time);
$seconds = 0;
for ($i = 0; count($timeParts); $i++)
{
$seconds += array_pop($timeParts) * 60 * $i;
}
return '<a href="javascript:void(0);" ' .
'onclick="var player = new YT.Player(\''.$id.'\', {events: {onReady: function …
Run Code Online (Sandbox Code Playgroud)